Jump to content

Echo a table form a database


Genesis730

Recommended Posts

I posted this a little bit ago, and was told to change a row field from "primary" to something else since primary is a key word... here is my updated code, i still get the same error...

 

"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource"

 

<table cellspacing="0" cellpadding="0" border="1">
  <tr>
    <td>ID</td><td>Name</td><td>Primary #</td><td>Secondary #</td><td>Address</td><td>City</td><td>Arizona</td><td>ZIP</td><td>Email Address</td><td>Diagnostic</td><td>Virus</td><td>Optimize</td><td>Repair</td><td>Setup</td><td>OS</td><td>Backup</td><td>Questions</td><td>Miles</td><td>Paid</td>
  </tr>
<?PHP
   $res = mysql_query("SELECT id, date, firstname, lastname, primaryphone, secondaryphone, address, city, state, zip, emailaddress, diagnostic, virus, optimize, repair, setup, os, backup, questions, miles, paid FROM contact ORDER BY date");

   while ($row = mysql_fetch_assoc($res)) {
      echo "<tr><td>{$row['id']}</td><td>{$row['firstname']} {$row['lastname']}</td><td>{$row['primaryphone']}</td><td>{$row['secondaryphone']}</td><td>{$row['address']}</td><td>{$row['city']}</td><td>{$row['state']}</td><td>{$row['zip']}</td><td>{$row['emailaddress']}</td><td>{$row['diagnostic']}</td><td>{$row['virus']}</td><td>{$row['optimize']}</td><td>{$row['repair']}</td><td>{$row['setup']}</td><td>{$row['os']}</td><td>{$row['backup']}</td><td>{$row['questions']}</td><td>{$row['miles']}</td><td>{$row['paid']}</td></tr>";
   }
?>

</table>

Link to comment
https://forums.phpfreaks.com/topic/189379-echo-a-table-form-a-database/
Share on other sites

Change your query so that it has the or die(trigger_error()); after it (temporarily) and post back the result.

 

 

   $res = mysql_query("SELECT id, date, firstname, lastname, primaryphone, secondaryphone, address, city, state, zip, emailaddress, diagnostic, virus, optimize, repair, setup, os, backup, questions, miles, paid FROM contact ORDER BY date") or trigger_error(mysql_error());

I did what you suggested Andy-H and got this error

"Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'optimize, repair, setup, os, backup, questions, miles, paid FROM contact ORDER B' at line 1"

 

$res = mysql_query("SELECT id, date, firstname, lastname, primaryphone, secondaryphone, address, city, state, zip, emailaddress, diagnostic, virus, optimize, repair, setup, os, backup, questions, miles, paid FROM contact ORDER BY date");

WAS CHANGED TO

$res = mysql_query("SELECT id, date, firstname, lastname, primaryphone, secondaryphone, address, city, state, zip, emailaddress, diagnostic, virus, optimize, repair, setup, os, backup, questions, miles, paid FROM contact ORDER BY date") or trigger_error(mysql_error());

 

Oh, I wrote it wrong, the new error is

 

"Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'optimize, repair, setup, os, backup, questions, miles, paid FROM contact' at line 1"

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

 

 

Looks like optimize is a mysql keyword, add the backticks (`) around the fieldname in your query as shown in the code below or change the table name.

   $res = mysql_query("SELECT id, date, firstname, lastname, primaryphone, secondaryphone, address, city, state, zip, emailaddress, diagnostic, virus, `optimize`, repair, setup, os, backup, questions, miles, paid FROM contact ORDER BY date") or trigger_error(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.