开发者

Trying to output the number of rows in sql to jquery

I have followed a tutorial to add comments asynchronously to a page. It all makes sense, but now I want to change it to display only a count of the number of comments in the mySql table. I have attempted changing it logically but I seem to be lost as to how to pass the data from the php to the jquery function properly?

Here is the original working comments code:

javascript

<script type="text/javascript">  

$(function() {  
    //开发者_开发百科retrieve comments to display on page  
  $.getJSON("comments.php?jsoncallback=?", function(data) {  


   //loop through all items in the JSON array  
   for (var x = 0; x < data.length; x++) {  

     //create a container for each comment  
     var div = $("<div>").addClass("row").appendTo("#comments");  

     //add author name and comment to container  
     $("<label>").text(data[x].name).appendTo(div);  
     $("<div>").addClass("comment").text(data[x].comment).appendTo(div);  
   } 
  });  
});  
</script>

And the comments.php

<?php  

  //db connection detils  
  $host = "localhost";  
  $user = "***";  
  $password = "***";  
  $database = "comments";  

  //make connection  
  $server = mysql_connect($host, $user, $password);  
  $connection = mysql_select_db($database, $server);  

  //query the database    
    $query = mysql_query("SELECT * FROM comments");  

    //loop through and return results  
    for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {  
        $row = mysql_fetch_assoc($query);  

        $comments[$x] = array("name" => $row["name"], "comment" => $row["comment"]);  
      }  

  //echo JSON to page  
  $response = $_GET["jsoncallback"] . "(" . json_encode($comments) . ")";  
  echo $response; 

?>

And this is how I tried to alter them:

javascript

<script type="text/javascript">  

$(function() {  
    //retrieve comments to display on page  
  $.getJSON("comments.php?jsoncallback=?", function(data) {  

     var div = $("<div>").addClass("row").appendTo("#comments");   
     $("<label>").text(data).appendTo(div);  


  });  
});  
</script>

And the comments.php

<?php  

  //db connection detils  
  $host = "localhost";  
  $user = "***";  
  $password = "***";  
  $database = "comments";  

  //make connection  
  $server = mysql_connect($host, $user, $password);  
  $connection = mysql_select_db($database, $server);  

  //query the database    
    $query = mysql_query("SELECT COUNT (*) FROM comments");  

    //return results  
    $numrows = mysql_num_rows($query); 

  echo $numrows; 

?>

This does not seem to work for me. I don't get any errors, but the result of the query is not being added to the page? Any help would be much appreciated, thanks in advance.


Going off of what @mellamokb said, you need to grab the first row (which contains the count), not the number of rows returned. Instead of $numrows = mysql_num_rows($query);, try just using something along the lines of $row = mysql_fetch_array($query); $count = $row[0];. $count should then be the number of comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜