Jump to content

[SOLVED] printing out mysql table


kev wood

Recommended Posts

i have found this code and would like to know how i can get the column titles to show up at the top of the page.

 

$result = mysql_query( "SELECT * FROM merc_users" ) 
or die("SELECT Error: ".mysql_error()); 
$num_rows = mysql_num_rows($result); 
print "There are $num_rows records.<P>"; 
print "<table width=1000 height=400 border=1>\n"; 
while ($get_info = mysql_fetch_row($result)){ 
print "<tr>\n"; 
foreach ($get_info as $field) 
print "\t<td><font face=arial size=2/>$field</font></td>\n"; 
print "</tr>\n"; 
} 
print "</table>\n"; 

 

this code simply just prints out all the data without the titles to the columns

Link to comment
https://forums.phpfreaks.com/topic/109733-solved-printing-out-mysql-table/
Share on other sites

thanks for the reply.  i used that function.  i has been having trouble with this site today keeps timing out when i try to load pages.  only just let me back in now.

 

i got all the names printed out for the titles of each column.

You really shuldn't be using FONT tags any more, but here you go:

 

<?php

$result = mysql_query( "SELECT * FROM merc_users" ) 
         or die("SELECT Error: ".mysql_error()); 

print "There are " . mysql_num_rows($result) . " records.<P>"; 
print "<table width=\"1000\" height=\"400\" border=\"1\">\n"; 

$showHeaders = true;
while ($get_info = mysql_fetch_row($result)){ 

 //Show headers on 1st pass
 if ($showHeaders) {
   print "<tr>\n";
   foreach ($get_info as $header => $field)
     print "\t<th><font face=\"arial\" size=\"2\">$header</font></th>\n"; 
   print "</tr>\n";
   $showHeaders = false;
 }

 //Display the record data
 print "<tr>\n";
 foreach ($get_info as $field)
   print "\t<td><font face=\"arial\" size=\"2\">$field</font></td>\n"; 
 print "</tr>\n"; 
}

print "</table>\n"; 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.