Log in

View Full Version : display mysql database rowcount using php,ajax and json



mutago
05-22-2014, 10:56 PM
.i want to display mysql database rows via ajax and json.but values is displaying "UNDEFINED". i know the problem is from ajax.html. any help please.

ajax.html


<script>
$(document).ready(function() {
//$("#ajaxButton").click(function() {



$.ajax({
type: "post",




url: "rowcount.php",
success: function(data) {
var obj = $.parseJSON(data);
var result = "<ul>"
$.each(obj, function() {
result = result + "<li><b><font size=5>your rowcount is</font> <font color=green size=5> " + this['row'] + "</font></b></li>";
});
result = result + "</ul>"
$("#result").html(result);
}
});


//});



});

</script>
<div id="result"></div>




rowcount.php



<?php


require('db.php');



$result = $db->prepare('SELECT * FROM users
');
$result->execute(array());

$rows = array();
//while ($r = $result->fetch()) {
//$rows[] = $r;
//}




$r = $result->rowCount();
$rows[] = $r;


echo json_encode($rows);

?>