我尝试创建json对象,但无法创建
||
在这里我有一些功能可以读取我的桌子
<?php
public function get_all_record($table, $fields = \"*\"){
$sql = \"SELECT $fields FROM $table\";
$result = $this->sqlordie($sql);
$xx=0;
while($row = mysql_fetch_assoc($result))
{
$myrow[$xx] = $row;
$xx++;
}
mysql_free_result($result);
return $myrow;
}
private function sqlordie($sql) {
$return_result = mysql_query($sql, $this->conn);
if($return_result) {
return $return_result;
} else {
$this->sql_error($sql);
}
}
private function sql_error($sql) {
echo mysql_error($this->conn).\'<br>\';
die(\'error: \'. $sql);
}
?>
下面的代码我调用get_all_record函数并返回结果,我使用json_encode转换为json对象
<?php
$myItem = get_all_record(\"mc_category\",\"category_id,category_name,category_description\");
echo json_encode($myItem);
?>
我得到如下输出
[{\"category_id\":\"2\",\"category_name\":\"book\",\"category_description\":\"all type of books\"},{\"category_id\":\"3\",\"category_name\":\"book\",\"category_description\":\"all type of books\"},{\"category_id\":\"4\",\"category_name\":\"Phone\",\"category_description\":\"All type of phones\"},{\"category_id\":\"5\",\"category_name\":\"Phone\",\"category_description\":\"All type of phones\"}]
但是我需要如下的JSON对象
{ \"aaData\": [
[\"Trident\",\"Internet Explorer 4.0\",\"Win 95+\",\"4\",\"X\"],
[\"Trident\",\"Internet Explorer 5.0\",\"Win 95+\",\"5\",\"C\"],
[\"Trident\",\"Internet Explorer 5.5\",\"Win 95+\",\"5.5\",\"A\"],
[\"Trident\",\"Internet Explorer 6\",\"Win 98+\",\"6\",\"A\"],
[\"Trident\",\"Internet Explorer 7\",\"Win XP SP2+\",\"7\",\"A\"]
] }
您能帮忙创建上述json对象吗?
没有找到相关结果
已邀请:
2 个回复
蓟类
殿虫