Show SQL table output using PHP

I found this code on the internet. This is a nice bit of code to show an arbitrary table with headers and all the entries. There is probably lots more one could do with it, but it works well for general testing and is completely independent of what is in the table.

[cc lang="php"]
function display($table)
{
$db_host = ‘localhost’;
$db_user = ‘‘;
$db_pwd = ‘ ‘;
 
$database = ‘‘;
 
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 “

;Table: {$table}

“;
echo “

“;
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "

“;
}
echo “

\n”;
// printing table rows
while($row = mysql_fetch_row($result))
{
echo “

“;
 
// $row is array… foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo “

“;
 
echo “

\n”;
}
echo “

{$field->name}
$cell

“;
mysql_free_result($result);
}
[/cc]