PHP/HTML Table output fails?
Posted: Wed Dec 16, 2009 7:04 am
HI Everyone
I'm going slightly insane trying to figure out why this code fails to retrieve any data.
It fails at this part.
I have tried numerous iterations of php code to retrieve data from a mysql database (php noob ya' see..) all to no avail.
Connection parameters are fine as far as I am aware (triple checked after I had triple checked) and presumably it would fail before the above code block if any of the connection parameters were incorrect.
So I hand this to expert sages (you!) in the hope that you may be able to help?
Kindest regards
Peter
I'm going slightly insane trying to figure out why this code fails to retrieve any data.
Code: Select all
<html><head><title>All Data</title></head><body>
<?php
$db_host = 'host';
$db_user = 'user';
$db_pwd = 'pwd';
$database = 'new_database';
$table = 'alldata';
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 {$adgroup_data}");
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);
?>
</body></html>Code: Select all
// sending query
$result = mysql_query("SELECT * FROM {$adgroup_data}");
if (!$result) {
die("Query to show fields from table failed");
}Connection parameters are fine as far as I am aware (triple checked after I had triple checked) and presumably it would fail before the above code block if any of the connection parameters were incorrect.
So I hand this to expert sages (you!) in the hope that you may be able to help?
Kindest regards
Peter