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

送分小仙女□ 提交于 2019-11-30 14:06:13
LeonanCarvalho

It's a Bug reported to PHP Developers #70384

The developer andrey@php.net 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+

I had the same problem in PHP 5.5 - decided to update to PHP 5.6, but the problem still existed. Casting to CHAR helps, but it isn't good solution.

My PHP configuration was using libmysqlclient and when I changed it to mysqlnd (MySQL native driver) it solved the problem.

So I recommend to do the same.

You can read about PHP MySQL drivers here: http://php.net/manual/en/mysqlinfo.library.choosing.php

I installed mysqlnd driver in Ubuntu server using apt-get:

apt-get install php5-mysqlnd

As a small addendum. Casting the column to a char like that has the effect of returning the value with double quotes around it. You can get rid of them using trim:

select *, TRIM(BOTH '"' FROM CAST(json_col as CHAR)) as json_col from table_with_json_type
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!