来自http://tunps.com/convert-db-result-set-to-json
代码片段
public function getSentBox($empId)
{
$sql=sprintf("select * from %smail_status ms left join %smail m on ms.mailid=m.mailid and fromid=%s",SQLPREFIX,SQLPREFIX,$empId);
$stmt=$this->db->query($sql);
$rows=array();
while(($r=$this->db->fetch_assoc($stmt)) == true){
$rows[]=$r;
}
return json_encode($rows);
}
|
开始傻傻的想通过每行数据利用字符拼凑起来,但是方法繁琐。
在
SO上发现此奇技淫巧,将结果集先转换成二维数组,然后json_encode之。
返回合法json数据:
[{"MAILID":"1","FROMID":"1","FROMSTATUS":"sent","FROMNAME":"\u7ba1\u7406\u5458","TOID":"2","TOSTATUS":"unread","TONAME":"\u5f20\u4e09","SENDTIME":"2010-05-07","MAILTITLE":"\u6807\u9898","MAILCONTENT":"\u5185\u5bb9"},{"MAILID":"1","FROMID":"1","FROMSTATUS":"sent","FROMNAME":"\u7ba1\u7406\u5458","TOID":"2","TOSTATUS":"read","TONAME":"\u5f20\u4e09","SENDTIME":"2010-05-07","MAILTITLE":"\u6807\u9898","MAILCONTENT":"\u5185\u5bb9"}]
|
This entry was posted in 未分类 and tagged
json. Bookmark the
permalink.