问一个mysql遍历转json的有关问题
程序员文章站
2022-05-07 07:49:11
...
问一个mysql遍历转json的问题.
我用PHP给Jquery AJAX传值.用$.parseJSON解析
如果要查一条记录,我已经学会了.如果要穿传多条记录.就有问题了.
//一条记录
//$row = mysql_fetch_array( $result );
//echo json_encode ($row);
//多条记录
while ($row = mysql_fetch_array($result)) {
echo json_encode($row);
};
按这个方法,如果有两条数据.回传这样一个json
{"0":"68","id":"68","1":"90","top":"90","2":"120","lef":"120","3":"u2","uid":"u2"}{"0":"92","id":"92","1":"40","top":"40","2":"320","lef":"320","3":"u1","uid":"u1"}
用$.parseJSON就无法解析了.请问有一个办法吗?
------解决方案--------------------
while ($row = mysql_fetch_array($result)) {
$res[] = $row;
}
echo json_encode($res);
我用PHP给Jquery AJAX传值.用$.parseJSON解析
如果要查一条记录,我已经学会了.如果要穿传多条记录.就有问题了.
//一条记录
//$row = mysql_fetch_array( $result );
//echo json_encode ($row);
//多条记录
while ($row = mysql_fetch_array($result)) {
echo json_encode($row);
};
按这个方法,如果有两条数据.回传这样一个json
{"0":"68","id":"68","1":"90","top":"90","2":"120","lef":"120","3":"u2","uid":"u2"}{"0":"92","id":"92","1":"40","top":"40","2":"320","lef":"320","3":"u1","uid":"u1"}
用$.parseJSON就无法解析了.请问有一个办法吗?
------解决方案--------------------
while ($row = mysql_fetch_array($result)) {
$res[] = $row;
}
echo json_encode($res);
相关文章
相关视频