表上的 PHP PDO 查询错误具有 json 数据类型 (MySQL 5.7.8-rc)

标签 php mysql pdo types mysql-json

我正在 mysql 5.7 上尝试新的 json 数据类型。当我使用 native php mysql查询时,它工作正常,但是当我使用PDO查询数据时,它显示此错误:

Error: exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2036 ' in /some_folder/pdo.php:12 Stack trace: #0 /some_folder/pdo.php(12): PDO->query('select * from table_has_json_datatype') #1 {main}

你们知道如何解决这个问题吗?

谢谢。

更新我的简单测试代码:

<?php

try{
    $db = new PDO('mysql:host=some.host;dbname=somedb;charset=utf8', 'user', 'pwd');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch(PDOException  $e){
    echo "Error1: ".$e;
}

try{
    $query = $db->query("select * from table_with_json_type");

}catch(PDOException $e){
    echo "Error2: ".$e;
}
?>

最佳答案

这是一个报告给 PHP 开发人员的错误 #70384

开发者 andrey@php.net 刚刚发布:

The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/.

For Windows:

http://windows.php.net/snapshots/ Thank you for the report, and for helping us make PHP better.

Fixed in PHP-5.6.19, PHP-7.0 and master (PHP-7.1)

Thank You for your report

因此,PHP 5.6.19+ 将支持 JSON 数据类型 对于其他版本,上面有一个解决方法。

此解决方法将具有 CAST 功能的 JSON 字段修改为 CHAR,从 PHP 的角度来看这很好: 例如:

select *, CAST(json_col as CHAR) as json_col from table_with_json_type

它在所有情况下都对我有用。

要完全兼容,您必须使用 PHP-5.6.19+

关于表上的 PHP PDO 查询错误具有 json 数据类型 (MySQL 5.7.8-rc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253778/

相关文章:

php - 从mysql动态选择列

php - 更新 PDO 绑定(bind)键和值

php - PDO MySQL:是否使用 PDO::ATTR_EMULATE_PREPARES?

php - 使用 MySQL 查找树中的级别数

php - 带有符号链接(symbolic link)和索引文件的 .htaccess 配置未按预期工作

php - 传递给函数的参数必须是可调用的,数组给定

PHP/AJAX - 当 MySQL 中出现新行时随时更新列表

javascript - 根据特定学号显示图片

php - 仅选择图像的特定部分

php - 需要指导以开始使用 Zend ACL