Jump to content

Trying to echo out a table


Genesis730

Recommended Posts

Anyone see why this wouldn't echo my table onto a webpage?

 

<table cellspacing="0" cellpadding="0" border="1">
  <tr>
    <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, firstname, lastname, primary, secondary, address, city, 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['firstname']} {$row['lastname']}</td><td>{$row['primary']}</td><td>{$row['secondary']}</td><td>{$row['address']}</td><td>{$row['city']}</td><td>Arizona</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>

 

The error message I get is "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource "

Link to comment
https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/
Share on other sites

The error means that the query failed and $res is a FALSE value instead of a result resource.

 

Of the several reasons a query can fail, your's is because primary is a reserved keyword. If you truly have a column named primary, you should either rename it or you must enclose it in back-ticks ` `

how exactly would I modify the code??

Here's what I changed it to...

 

<table cellspacing="0" cellpadding="0" border="1">
  <tr>
    <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, firstname, lastname, `primary`, secondary, address, city, 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['firstname']} {$row['lastname']}</td><td>{$row[`primary`]}</td><td>{$row['secondary']}</td><td>{$row['address']}</td><td>{$row['city']}</td><td>Arizona</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>

heres another was which helps me see it better, just move in and out of php to html and it becomes a little clearer at least to me, i didnt fill in all the fields just made it generic so you can get the idea. for me its less cluttered and easier to see mistakes (typos or otherwise)

 

   
<?php
while ($row = mysql_fetch_assoc($res)) {
   	?>
   	<tr>
   	<td><?php echo $row['firstname'] ?> </td>
   	<td><?php echo $row['lastname'] ?> </td>
   	<td><?php echo $row['next column'] ?> </td>
   	<td><?php echo $row['next column'] ?> </td>
   	<td><?php echo $row['next column'] ?> </td>
   	<td><?php echo $row['next column'] ?> </td>
   	<td><?php echo $row['next column'] ?> </td>
   	</tr>
   	<?php
   	}
?>

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.