Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO query error on table has json data type (MySQL 5.7.8-rc)

I'm trying new json datatype on mysql 5.7. When I use native php mysql query, it works fine, but when I use PDO to query data, it shows this error:

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}

Do you guys know how to solve this problem ?

Thanks.

Update with my simple test code:

<?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;
}
?>
like image 518
Quang Ng. Avatar asked Oct 21 '15 07:10

Quang Ng.


1 Answers

It's a Bug reported to PHP Developers #70384

The developer [email protected] just posted:

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

So, the JSON data type will be supported on PHP 5.6.19+ For other version there is a workaround available above.

This workaround modify the JSON field with CAST feature to a CHAR, which is fine from PHP's perspective: Eg.:

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

It's worked to me in all cases.

To Full compatibility you must use PHP-5.6.19+

like image 54
LeonanCarvalho Avatar answered Sep 27 '22 17:09

LeonanCarvalho