Snippet Name: Show mySQL table as HTML
Description: This example use mySQL functions from PHP to display a full mySQL table (every column and every row) as HTML.
Comment: (none)
Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: February 28th, 2009
|
<?PHP
$table = 'spheres';
IF (!mysql_connect($db_host, $db_user, $db_pwd))
DIE("Can't connect to database");
IF (!mysql_select_db($database)) DIE("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
IF (!$result) {
DIE("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
ECHO "<h1>Table: {$table}</h1>";
ECHO "<table border='1'><tr>";
// printing table headers
FOR($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
ECHO "<td>{$field->name}</td>";
}
ECHO "</tr>\n";
// printing table rows
WHILE($row = mysql_fetch_row($result))
{
ECHO "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
FOREACH($row AS $cell)
ECHO "<td>$cell</td>";
ECHO "</tr>\n";
}
mysql_free_result($result);
?> |