开发者

json not outputting

开发者 https://www.devze.com 2023-03-06 11:23 出处:网络
开发者_如何学CI\'m running this code in php while ($row = mysql_fetch_array($result)) { $arr = array(\"joke\" => $row[\'joke\'], \"date\" => $row[\'date\'], \"rating\" => $row[\'rating\']);
开发者_如何学C

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

0

精彩评论

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

关注公众号