php - Trying to output the number of rows in sql to jquery -


i have followed tutorial add comments asynchronously page. makes sense, want change display count of number of comments in mysql table. have attempted changing logically seem lost how pass data php jquery function properly?

here original working comments code:

javascript

<script type="text/javascript">    $(function() {       //retrieve comments display on page     $.getjson("comments.php?jsoncallback=?", function(data) {        //loop through items in json array      (var x = 0; x < data.length; x++) {         //create container each comment        var div = $("<div>").addclass("row").appendto("#comments");         //add author name , comment container        $("<label>").text(data[x].name).appendto(div);        $("<div>").addclass("comment").text(data[x].comment).appendto(div);      }    });   });   </script> 

and 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 database         $query = mysql_query("select * comments");        //loop through , return results       ($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 page     $response = $_get["jsoncallback"] . "(" . json_encode($comments) . ")";     echo $response;   ?> 

and how tried alter them:

javascript

<script type="text/javascript">    $(function() {       //retrieve comments display on page     $.getjson("comments.php?jsoncallback=?", function(data) {         var div = $("<div>").addclass("row").appendto("#comments");         $("<label>").text(data).appendto(div);       });   });   </script> 

and 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 database         $query = mysql_query("select count (*) comments");        //return results       $numrows = mysql_num_rows($query);     echo $numrows;   ?> 

this not seem work me. don't errors, result of query not being added page? appreciated, in advance.

going off of @mellamokb said, need grab first row (which contains count), not number of rows returned. instead of $numrows = mysql_num_rows($query);, try using along lines of $row = mysql_fetch_array($query); $count = $row[0];. $count should number of comments.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -