PHP PDO 无法在数据库中获取 json 值


PHP PDO can't get json value in database

我尝试使用 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","answers":["6032"]},"topicId":"1593","answers":["8122"]},{"topicId":"1598","answers":["6064"]},{"topicId":"1601","answers":["6073"]}]

结果:

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

您正在获取的指示您的查询未返回任何记录。 在普通查询中,您将获得一个空集,但是,由于您使用的是聚合函数,因此当查询未选择任何记录时,您将获得所有 null。这里的问题是您的 WHERE 子句可能会消除表中的所有记录。

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

$questionCorrectAnswer = $query3->fetch();

$questionCorrectAnswer = $query3->fetch(PDO::FETCH_OBJ);

而不是

questionCorrectAnswer['answer'];

我建议尝试

questionCorrectAnswer->answer;

此外,当您存储 Json 对象时,它可能会获得 Print_r($foo) 或 Var_Dump($foo) 的回报,以防数组到字符串转换失败。