json not outputting
I'm running this code in php
while ($row = mysql_fetch_array($result))
{
$arr = array("joke" => $row['joke'], "date" => $row['date'], "rating" => $row['rating']);
echo json_encode($arr);
}
but there's no output. I am running php 5.3.6
Most likely your query's failing. Either due to syntax errors or just not matching anything. Redo your code so it looks something like this:
$sql = "...";
$result = mysql_query($sql) or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);
The or die
portion will handle the case when the query's bad and causes an error. setting $data to an empty array initially ensures that you'll get SOMETHING out of json_encode, even if it's just an empty javascript array. And then the while loop sucks the query results and stuffs into the $data array.
nvm I figured it out. the way to do this is to use sql2json
精彩评论