我正在尝试从我的 mysqli 结果构建一个 json 对象。我该怎么做。目前它不会创建一个看起来像 json 的对象。
这是我的代码:
$result = $dataConnection->prepare("SELECT id, artist, COUNT(artist) AS cnt FROM {$databasePrefix}users GROUP BY artist ORDER BY cnt DESC LIMIT 0 , 30");
$result->execute();
if($result->error)
{
die("That didn't work. I get this: " . $result->error);
}
$result->bind_result($id, $artist, $count);
$data = array();
while($result->fetch()){
$data[] = '{ id :'.$id.', artist :'.$artist.', count :'.$count.'}';
}
echo json_encode($data);
$dataConnection->close();
我想要一个数据对象,例如:
{"id":"27","artist":"myArtist","count":"29"},....