Genesis730 Posted January 21, 2010 Share Posted January 21, 2010 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 " Quote Link to comment https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2010 Share Posted January 21, 2010 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 ` ` Quote Link to comment https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/#findComment-999659 Share on other sites More sharing options...
Genesis730 Posted January 22, 2010 Author Share Posted January 22, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/#findComment-999664 Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2010 Share Posted January 22, 2010 Only the offending column name where it appears in the query is enclosed in back-ticks. The php code would not be affected. Quote Link to comment https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/#findComment-999826 Share on other sites More sharing options...
gevensen Posted January 22, 2010 Share Posted January 22, 2010 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/189377-trying-to-echo-out-a-table/#findComment-999832 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.