Click here to Skip to main content
14,694,507 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello guys,
I am getting a problem in getting JSON data from a php file (PHP file is using database i.e MySQL).
Code is mentioned below.

jQuery
$.getJSON("fetch.php",function(data){
    $("ul").empty();
    $.each(data.result,function()
{
   $("ul").append("<li>Name : "+this['name']+"</li><br/> ");
   $("ul").append("<li>Age : "+this['age']+"</li><br/> ");
   $("ul").append("<li>Company : "+this['company']+"</li><br/> ");
});
});



PHP
<?php
include_once ('db.php');

$sql="Select * from people";
$res=  mysql_query($sql);
$result=array();

while($row=  mysql_fetch_array($res))
{
    array_push($result, array('name' => $row[0],
                              'age'=>$row[1],
                               'company'=>$row[2]));
    echo json_encode(array("result"=>$result));
}
?>



Update : Problem is that jquery doesn't gives any output on the browser. Yes, I am posting every row of the query output so I inserted "echo" in while loop.
Posted
Updated 7-Mar-14 2:01am
v2
Comments
Maarten Kools 5-Mar-14 9:04am
   
You didn't mention your problem. But I would suggest moving the echo json_encode call out of your while loop, otherwise you're outputting for each row.
Killzone DeathMan 6-Mar-14 5:05am
   
Agree :)
Akkywadhwa 7-Mar-14 7:02am
   
Hello Maarten,
I have updated my question. Kindly check this.
Thanks,
Akky
Maarten Kools 7-Mar-14 7:21am
   
If you are outputting every row of the query your json isn't going to be correct. You can debug your JavaScript in various browsers with built in tools or extensions (in the case of Firefox)
Internet Explorer[^]
Firefox[^]
Chrome[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900