JSON value encoded twice : how to use fetch_assoc()?(JSON 值编码两次:如何使用 fetch_assoc()?)
问题描述
以下代码返回值两次,一次以 JSON 编码:
prepare('SELECT Date, Open, Close FROM quotes WHERE Symbol = ? AND Date > ? AND Date < ?');$req->execute(array($_GET['id'], $_GET['datemin'], $_GET['datemax']));$测试=数组();while ($donnees = $req->fetch()){$test[] = $donnees;}回声 json_encode($test);?>
<块引用>
[{"日期":"2012-02-29","0":"2012-02-29","开盘":"88.14","1":"88.14","收盘":"87.60","2":"87.60"},{"日期":"2012-02-28","0":"2012-02-28","开盘":"87.83","1":"87.83","收盘":"87.77","2":"87.77"},{"日期":"2012-02-27","0":"2012-02-27","开盘":"87.41","1":"87.41","关闭":"88.07","2":"88.07"}]
我阅读了 一些帖子 我有使用 fetch_assoc()
而不是 fetch_array()
.
但下面的代码什么也不返回:while ($donnees = $req->fetch_assoc())
.这个也没有:while ($donnees = $req->fetch_array())
.
我不明白这是怎么回事.
参见手册.
http://www.php.net/manual/en/pdostatement.fetch.php
你应该试试:
$req->fetch(PDO::FETCH_ASSOC)
The following code returns the value twice, once encoded in JSON :
<?php
$req = $bdd->prepare('SELECT Date, Open, Close FROM quotes WHERE Symbol = ? AND Date > ? AND Date < ?');
$req->execute(array($_GET['id'], $_GET['datemin'], $_GET['datemax']));
$test=array();
while ($donnees = $req->fetch())
{
$test[] = $donnees;
}
echo json_encode($test);
?>
[{"Date":"2012-02-29","0":"2012-02-29","Open":"88.14","1":"88.14","Close":"87.60","2":"87.60"},{"Date":"2012-02-28","0":"2012-02-28","Open":"87.83","1":"87.83","Close":"87.77","2":"87.77"},{"Date":"2012-02-27","0":"2012-02-27","Open":"87.41","1":"87.41","Close":"88.07","2":"88.07"}]
I read on some post I have to use fetch_assoc()
instead of fetch_array()
.
But the following code returns nothing : while ($donnees = $req->fetch_assoc())
. Nor does this one : while ($donnees = $req->fetch_array())
.
I don't get what's wrong.
See manual.
http://www.php.net/manual/en/pdostatement.fetch.php
You should try:
$req->fetch(PDO::FETCH_ASSOC)
这篇关于JSON 值编码两次:如何使用 fetch_assoc()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON 值编码两次:如何使用 fetch_assoc()?


基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01