Click to See Complete Forum and Search --> : display mysql results using PHP
ssffcc
04-12-2006, 01:39 PM
hi, I have a query to retrieve all the records from mysql table "mybooks":
select * from mybooks
the result contains more than one rows, I want to use PHP to display all the results in HTML table format.
I am new to PHP and MySql, not sure how to realize this.
plz help
aaronbdavis
04-12-2006, 01:46 PM
start here (http://www.php-mysql-tutorial.com/php-mysql-select.php)
ssffcc
04-12-2006, 01:58 PM
thx veryvery much aaron, great help!!!!!! :)
bokeh
04-12-2006, 02:10 PM
function print_table($tablename)
{
$query = "SELECT * FROM $tablename";
$query_result = mysql_query($query) or die (mysql_error());
if(mysql_num_rows($query_result) > 0){
echo '<table class="mysql_table" border="1" cellspacing="0" cellpadding="6">'."\n".
'<caption>Table name: "'.$tablename.'"</caption>'."\n";
$first_time = true;
while($row = mysql_fetch_assoc($query_result)){
if($first_time){
$first_time = false;
$headings = array_keys($row);
echo '<tr>';
foreach($headings as $heading){
echo '<th>'.$heading.'</th>';
}
echo '</tr>'."\n";
}
echo '<tr>';
foreach($headings as $heading){
echo '<td>';
echo($row[$heading]) ? $row[$heading] : ' ';
echo '</td>';
}
echo '</tr>'."\n";
}
echo '</table>'."\n";
}else{
echo 'The table "'.$tablename.'" does not have anything in it yet'."\n";
}
}
ssffcc
04-12-2006, 03:20 PM
thx bokeh, I notified the mysql_fetch_assoc is a great function, thx again~