PDO query mysql returns field integer type into String type solution
When using PDO to query mysql database, after executing prepare and execute, the returned field data will all change into character type .
For example, the id in the database is Int, and the query returns the type String.
For weakly typed languages like php, it has little impact. When the API returns data, if the type is inconsistent with the database, the impact will be great for strong types such as Java and Objective C.
Solution:
<?php$pdo = new PDO($dsn, $user, $pass, $param);// 在创建连接后,加入$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);?>
The role of these two settings
PDO::ATTR_STRINGIFY_FETCHES Convert the value to a string when extracting.
PDO::ATTR_EMULATE_PREPARES Enables or disables emulation of prepared statements.
MorePDO setAttributePlease refer to: php Chinese website
This article explains the solution to change the integer type of the field returned by mysql through PDO to the String type, more related Please pay attention to php Chinese website for content.
Related recommendations:
How to use glob Methods related to traversing all files in a folder
Explanation on PHP floating point number comparison method
The above is the detailed content of The solution to change the integer type of the field returned by querying mysql through PDO to the String type. For more information, please follow other related articles on the PHP Chinese website!