Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The correct forum

Status
Not open for further replies.

ianedwards

IS-IT--Management
Oct 1, 2002
90
Hi

my first foray into php, mysql and wen design.Can someone tell me if I'm using the correct forum for advice.

What I want to do is populate a php web page from data in mysql but import each record into a table cell? the idea is that I can format the web page using css etc.

Think I've explained my self clearly

thanks

Ian
 
Hi

Your question can be answered here for now, but later you may have to repost in other forums for some specific help.

This simply displays the content of a table on a page :
Code:
<html>
<body>
<table>
<?php
$conn=mysql_connect([green][i]hostname[/i][/green],[green][i]username[/i][/green],[green][i]password[/i][/green]);
mysql_select_db([green][i]database[/i][/green]);
$res=mysql_query("select * from [green][i]tablename[/i][/green]");
$nr=0;
while ($data=mysql_fetch_row($res)) {
  if (!$nr++) {
    echo "<tr><th>Nr</th>";
    for ($i=0;$i<count($data);$i++) echo "<th>".mysql_field_name($res,$i)."</th>";
    echo "</tr>\n";
  }
  echo "<tr><td>$nr</td>";
  for ($i=0;$i<count($data);$i++) echo "<td>$data[$i]</td>";
  echo "</tr>\n";
}
?>
</table>
</body>
</html>

Feherke.
 
an alternative to Feherke's code which uses the (imo more usual) mysql_fetch_assoc method

Code:
<?php
//turn on error reporting for debugging
ini_set("display_errors", "on");
error_reporting(E_ALL);

//connect to the dbserver and database
mysql_connect(hostname,username,password)
	or die(mysql_error());
mysql_select_db(database)
	or die(mysql_error());

//get the data
$query = "Select * from tablename";
$result = mysql_query($query)
	or die(mysql_error());

//if no rows then give some helpful output and kill the script
if (mysql_num_rows($result) === 0){
  die ("no records to display");
}

//start building the table
echo "<table>\r\n";
$firstRow = true;
//step through each row of the database table and plot the output
while ($row=mysql_fetch_assoc($result)) {
    //test to see whether we need to output row headings
	if ($firstRow){
		$fr=''; //create a holding variable
	}
	$r = ''; //holding var for row
	foreach ($row as $key=>$val){
		$r .= "\t\t<td>$val</td>\r\n"; //the \t is to create a tab \r\n is for a new line
		if (isset($fr)){
			$fr .= "\t\t<th>$key</th>\r\n";
		}
	}    
	if (isset($fr)){
		echo "\t<tr>$fr</tr>\r\n"; 	//output the first row of headers
		$firstRow = false;	// stop subsequent output
		unset($fr); //kill the $fr variable
	}
	//output the data row
	echo "\t<tr>$r</tr>\r\n";
} //close the while loop
//close the table
echo "</table>\r\n";
?>
 
Hi

jpadie said:
the (imo more usual) mysql_fetch_assoc method
I would say it is not only your opinion. I also prefer [tt]mysql_fetch_assoc()[/tt] in my work. I decided to use [tt]mysql_fetch_row()[/tt] above for clearness. Hmm... Maybe was not so good idea.

Feherke.
 
thought it was a fine idea. no disrespect intended - i just wanted to present an alternative.
 
Hi

I just confirmed that [tt]mysql_fetch_assoc()[/tt] is indeed more usual. :)

And while you commented your code, your post would be welcomed even if it would show any disrespect.

Sorry, seems that my English skill is not at my best today.

Feherke.
 
Thanks one and all, I'll have to spend more time onnthe learning curve

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top