Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO可以';无法在数据库中获取json值_Php_Mysql_Json_Pdo - Fatal编程技术网

PHP PDO可以';无法在数据库中获取json值

PHP PDO可以';无法在数据库中获取json值,php,mysql,json,pdo,Php,Mysql,Json,Pdo,我尝试使用pdo来获取存储在数据库中的值。 但我不知道为什么这个值似乎不能响应它 这是我的密码: $_GET['id']='138b39bbef558cf44b3d222a6fb4d6b6'; $query3 = $conn->prepare("SELECT id, MAX(time),answer FROM `answer` where nodes_uuid = :nodeuuid and user_id = 101"); $query3->

我尝试使用pdo来获取存储在数据库中的值。 但我不知道为什么这个值似乎不能响应它

这是我的密码:

    $_GET['id']='138b39bbef558cf44b3d222a6fb4d6b6';
        $query3 = $conn->prepare("SELECT id, MAX(time),answer FROM `answer` where nodes_uuid = :nodeuuid and user_id = 101");
        $query3->bindValue(':nodeuuid', $_GET['id'], PDO::PARAM_STR); 
        $query3->execute(); 
$questionCorrectAnswer = $query3->fetch(); 
        echo $questionCorrectAnswer['answer'];
表:

    id         MAX(time)                  answer
----------    -------------------        ------------ 
    40        2015-02-25 18:18:53        [{"topicId":"1590","ans":["6032"]},"topicId":"1593","ans":["8122"]},{"topicId":"1598","ans":["6064"]},{"topicId":"1601","ans":["6073"]}]

我认为这里的错误是你得到了一个未定义的索引错误。您可以尝试交换:

$questionCorrectAnswer = $query3->fetch();
为了

而不是

questionCorrectAnswer['answer']

我建议你试试

questionCorrectAnswer->answer

此外,在存储Json对象时,如果数组到字符串的转换失败,它可能会支付Print\r($foo)或Var\u Dump($foo)的费用

结果:

array(6) { ["id"]=> NULL [0]=> NULL ["MAX(time)"]=> NULL [1]=> NULL ["answer"]=> NULL [2]=> NULL }

您得到的信息表明您的查询没有返回任何记录。但是,在普通查询中,您会得到一个空集,因为您使用的是聚合函数,所以当查询未选择任何记录时,您会得到所有空值。这里的问题是WHERE子句可能会删除表中的所有记录。

始终。您可能至少会收到一条
警告:未定义索引“answer”
消息
PDOStatement::fetch()
返回
false
错误,请使用
进行反检查==
.xdebug或var_dump告诉我们关于
$questionCorrectAnswer['answer']
的信息是什么?
$conn
的值是什么?@thecodese$conn=new-PDO(“mysql:host=$servername;dbname=mydb”,$username,$password)@LiouJay有结果显示吗?您打开错误报告了吗?谢谢,您的评论。但是数据在数据库中。在我发布这个问题之前,我检查了我的数据库。@LiouJay可能是这样,但是节点_uuid='138b39bbef558cf44b3d22a6fb4d6b6'和用户_id=101的组合是否出现在同一条记录上?是的,结果是一样的。@deceze,因为他的查询中有一个聚合函数,他会得到所有的空值。@jpheldson当然,很好的回答。。。你应该在回答中澄清这一点。或者使用
$query3->fetch(PDO::fetch_ASSOC)
$questionCorrectAnswer['answer']
的确,我只是更喜欢->而不是[''],我只是觉得它更漂亮。现在我们已经看到他的Var Dump是空的。@Andrew结果如下:array(3){[“id”]=>NULL[“MAX(time)”]=>NULL[“answer”]=>NULL}结果存在,并且是正确的关联数组-这意味着查询已成功执行并获取。但数组中的值不是预期的值。问题似乎出现在查询
中,因为选择了错误的行。
array(6) { ["id"]=> NULL [0]=> NULL ["MAX(time)"]=> NULL [1]=> NULL ["answer"]=> NULL [2]=> NULL }