Print SQL Query Results to Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kevin11phi
    New Member
    • Oct 2011
    • 7

    Print SQL Query Results to Table

    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.... :
    Code:
    $con = mysql_connect("localhost", "root", "mysqlr0x");
    if (!$con)
    {
    	die('Could not connect: ' . mysql_error());
    }
    
    $query = "SELECT id, name FROM inventory ORDER by basenet_id";
    $results = mysql_query($query, $con);
    
    while ($row = mysql_fetch_results($results)) {
    	echo "<tr>";
    	echo "<td>" .$row['name']."</td>";
    	echo "<td>"
    	echo "</table>";
    }
    
    mysql_close($con);
    Any suggestions?
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    I dont know any mysql_fetch_res ults or mysql_fetch_res ult
    Try
    mysql_fetch_row or
    mysql_fetch_ass oc

    there is another
    mysql_fetch_arr ay

    Comment

    • kevin11phi
      New Member
      • Oct 2011
      • 7

      #3
      I updated my code to reflect what you saw... and implemented the ping test (and made it into an html page).
      Code:
      <html>
      <head></head>
      <body class="page_bg">
      <?php #!/usr/bin/php
      $con = my_sql_connect("localhost", "root", "dud351");
      if (!$con) {
      	die('Could not connect: ' . mysql_error();
      }
      $query = "SELECT id, name FROM inventory ORDER by basenet_id";
      $results = mysql_query($query, $con);
      
      function ping($host){
      	exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, 
      $rval));
      	return rval === 0;
      }
      
      while($row = mysql_fetch_assoc($results)) {
      	echo "<tr>";
      	$checkThisVariable = ping($row['name']);
      	if ($checkThisVariable == 1) {
      		echo "<td>" . $row['name'] . " is up!</td>";
      	}
      	else {
      		echo "<td>" . $row['name'] . " is down!</td>";
      	}
      	echo "</table>";
      }
      
      mysql_close($con);
      ?>
      </body>
      </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:
      Code:
      "; $checkThisVariable = ping($row['name']); if ($checkThisVariable == 1) { echo "" . $row['name'] . " is up!"; } else { echo "" . $row['name'] . " is down!"; } echo ""; } mysql_close($con); ?>
      Last edited by kevin11phi; Oct 25 '11, 11:18 PM. Reason: New stuff

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        your code is not suppose to compile at all, look at line 7

        there is a syntax error.

        Comment

        • kevin11phi
          New Member
          • Oct 2011
          • 7

          #5
          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?

          Comment

          • bektorkhan
            New Member
            • Oct 2011
            • 1

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

            At a quick glance should be:

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

            Comment

            Working...