仍在尝试了解mysql到json的数据

时间:2014-03-29 18:13:26

标签: javascript php json

这个PHP应该返回4行数据....    行A,行B,行C,行D

$query = $mysqli->query("select codeid,description from codeTable where tableCode='TABLE'");
$json = array();
if($query->num_rows){
    while($tblArray[] = $query->fetch_object()){
    $json[]=$tblArray;
    }
}
echo json_encode($json);

但实际上,json_encode显示了10行数据....    A行,    A行,B行,    A行,B行,C行,    行A,行B,行C,行D

[[{"codeid":"4","description":"Document Type"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"},{"codeid":"1","description":"Note Type"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"},{"codeid":"1","description":"Note Type"},{"codeid":"5","description":"Projects"}]]

有人能告诉我为什么会这样吗?最终目标是将查询返回的四行数据放入javascript数组中。

1 个答案:

答案 0 :(得分:1)

// remove [] here, or you are adding the row data to an array, then add to another array.
while($tblArray = $query->fetch_object()){
   $json[] = $tblArray;
}