I am trying to display data from one of my mysql table's and am getting the error: Query to show fields from table
failed. The table is all within the database learndb. Other tables within this database have been working fine. The
only difference between the all table and the others is that it has a field called id which is a primary key, an
integer, and set to auto_increment.
First section of php:
<?php
$db_host = 'mysql.dan.housch.com';
$db_user = '***';
$db_pwd = '****';
$database = 'learndb';
$table = 'all';
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");
}
?>
Second and last instance of php:
<?php
$fields_num = mysql_num_fields($result);
echo "<table border='0'><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);
?>