JSON值编码两次:如何使用fetch_assoc()? [英] JSON value encoded twice : how to use fetch_assoc()?

查看:143
本文介绍了JSON值编码两次:如何使用fetch_assoc()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码一次返回值两次,一次使用JSON编码:

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,"关闭:" 88.07," 2:" 88.07}]

[{"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"}]

我读了一些帖子使用fetch_assoc()代替fetch_array().

但是以下代码不返回任何内容:while ($donnees = $req->fetch_assoc()).这也不是:while ($donnees = $req->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.

推荐答案

请参见手册.
http://www.php.net/manual/zh/pdostatement.fetch.php

See manual.
http://www.php.net/manual/en/pdostatement.fetch.php

您应该尝试:

$req->fetch(PDO::FETCH_ASSOC)

这篇关于JSON值编码两次:如何使用fetch_assoc()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆