无视安全问题(暂时)
我正在使用json_encode来显示选择查询的内容。 我用过:
$sql= {query code};
// created a temp array to hold data and an array for all the results
$resultArray = array();
$tempArray = array();
// looped through every object in the result set
while ($row = $result->fetch_object());
// added it into the results array
$tempArray = $row;
array_push($resultsArray, $temparray);
// encoded the array to a json format
echo json_encode($resultArray);
问题:我的对象没有名称,因此我很难编写任何可以读取结果并显示结果的代码。如何命名对象?这是我可以添加到表中的条目(假设我可以为每个对象提供不同名称的情况)
答案 0 :(得分:0)
事实上,这根本不起作用。将PDO
(或MySQLi
(如评论中所述)对象转换为JSON
。你应该使用fetch_assoc
instad,它将返回可以直接放在JSON结构中的关联数组。
答案 1 :(得分:0)
您应该尝试在PHP中构建所需的数组/对象结构。这样,当你json_encode
时,你就会知道它的样子,这将是你想要的。
尝试:
echo json_encode(array('people' => $resultArray));