javascript 如何从 mysqli 结果构建正确的 json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16817243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:05:31  来源:igfitidea点击:

How can I build a correct json from mysqli result?

phpjavascriptjsonmysqli

提问by prog_24

I am trying to build a json object from my mysqli result. How do I go about it. At the moment it does not create a json looking object.

我正在尝试从我的 mysqli 结果构建一个 json 对象。我该怎么做。目前它不会创建一个看起来像 json 的对象。

Here is my code:

这是我的代码:

$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();

I want a data object like:

我想要一个数据对象,如:

{"id":"27","artist":"myArtist","count":"29"},....

回答by Your Common Sense

$result = $dataConnection->query("SELECT id, artist, COUNT(artist) AS count FROM {$databasePrefix}users GROUP BY artist ORDER BY cnt DESC LIMIT 0 , 30");
$data = array();
while($row = $result->fetch_assoc()){
    $data[] = $row;
}
echo json_encode($data);

To tell you truth, mysqli is awful API to be used right in the application code.

说实话,mysqli 是在应用程序代码中使用的糟糕的 API。

Do yourself a favor and use at least PDO

帮自己一个忙,至少使用PDO

$result = $dataConnection->prepare("SELECT id, artist, COUNT(artist) AS count FROM {$databasePrefix}users GROUP BY artist ORDER BY cnt DESC LIMIT 0 , 30");
$result->execute();
echo json_encode($result->fetchAll());

unlike mysqli, it's methods always works.

与 mysqli 不同,它的方法总是有效的。

回答by Orangepill

Don't build your json for the values array that you will call json_encode on

不要为将在其上调用 json_encode 的 values 数组构建 json

instead of:

代替:

$data[] = '{ id :'.$id.', artist :'.$artist.', count :'.$count.'}';

do

$data[] = array("id"=>$id, "artist"=>$artist, "count"=>$count);

回答by BenRoe

If you use mysqli here is an example. I use it in combination with a javascript ajax call.
The output looks like this: [{"field1":"23","field2":"abc"},{"field1":"24","field2":"xyz"}]

如果您使用 mysqli,这里是一个示例。我将它与 javascript ajax 调用结合使用。
输出如下所示: [{"field1":"23","field2":"abc"},{"field1":"24","field2":"xyz"}]

$mysqli = mysqli_connect('localhost','dbUser','dbPassword','dbName');

/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$query = "SELECT field FROM table LIMIT 10";

if ($result = mysqli_query($mysqli, $query)) {
  $out = array();

  while ($row = $result->fetch_assoc()) {
    $out[] = $row;
  }

  /* encode array as json and output it for the ajax script*/
  echo json_encode($out);

  /* free result set */
  mysqli_free_result($result);

  /* close connection*/
  $mysqli->close();
}

/* close connection*/
$mysqli->close();

回答by Populus

just create an array of all your rows, then do:

只需创建一个包含所有行的数组,然后执行以下操作:

echo json_encode($array)