By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

Print SQL Query Results to Table

P: 7
Hey Everyone!!

I am a super noob at php. Also, I am having a bit of a problem with a small project I am working on. The project is simple: use a sql db to list all of the inventory and then test whether or not each machine from the inventory is up (i.e. ping-able). I have only implemented accessing the database but I cannot get php to print the results to a HTML table.

Here is what I have so far.... :
Expand|Select|Wrap|Line Numbers
  1. $con = mysql_connect("localhost", "root", "mysqlr0x");
  2. if (!$con)
  3. {
  4.     die('Could not connect: ' . mysql_error());
  5. }
  6.  
  7. $query = "SELECT id, name FROM inventory ORDER by basenet_id";
  8. $results = mysql_query($query, $con);
  9.  
  10. while ($row = mysql_fetch_results($results)) {
  11.     echo "<tr>";
  12.     echo "<td>" .$row['name']."</td>";
  13.     echo "<td>"
  14.     echo "</table>";
  15. }
  16.  
  17. mysql_close($con);
Any suggestions?
Oct 25 '11 #1
Share this Question
Share on Google+
5 Replies


100+
P: 1,059
I dont know any mysql_fetch_results or mysql_fetch_result
Try
mysql_fetch_row or
mysql_fetch_assoc

there is another
mysql_fetch_array
Oct 25 '11 #2

P: 7
I updated my code to reflect what you saw... and implemented the ping test (and made it into an html page).
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head></head>
  3. <body class="page_bg">
  4. <?php #!/usr/bin/php
  5. $con = my_sql_connect("localhost", "root", "dud351");
  6. if (!$con) {
  7.     die('Could not connect: ' . mysql_error();
  8. }
  9. $query = "SELECT id, name FROM inventory ORDER by basenet_id";
  10. $results = mysql_query($query, $con);
  11.  
  12. function ping($host){
  13.     exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, 
  14. $rval));
  15.     return rval === 0;
  16. }
  17.  
  18. while($row = mysql_fetch_assoc($results)) {
  19.     echo "<tr>";
  20.     $checkThisVariable = ping($row['name']);
  21.     if ($checkThisVariable == 1) {
  22.         echo "<td>" . $row['name'] . " is up!</td>";
  23.     }
  24.     else {
  25.         echo "<td>" . $row['name'] . " is down!</td>";
  26.     }
  27.     echo "</table>";
  28. }
  29.  
  30. mysql_close($con);
  31. ?>
  32. </body>
  33. </html>
Anything else that I am doing wrong?
However, the code still does not print out a table with the results of my query.

EDIT: After attempting to run the page from my webserver, I got this:
Expand|Select|Wrap|Line Numbers
  1. "; $checkThisVariable = ping($row['name']); if ($checkThisVariable == 1) { echo "" . $row['name'] . " is up!"; } else { echo "" . $row['name'] . " is down!"; } echo ""; } mysql_close($con); ?>
Oct 25 '11 #3

100+
P: 1,059
your code is not suppose to compile at all, look at line 7

there is a syntax error.
Oct 26 '11 #4

P: 7
I caught the syntax error but I forgot to change it in the code section of my post. The code seems to quit at the point where I need it to print my results associative array. Did I do something wrong?
Oct 26 '11 #5

P: 1
echo "<td>" . $row['name'] . " is up!</td>";

At a quick glance should be:

echo "<td>" . $row["name"] . " is up!</td>";
Oct 27 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.