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
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 ` `

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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
   	}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.