Home > Database > Mysql Tutorial > The solution to change the integer type of the field returned by querying mysql through PDO to the String type

The solution to change the integer type of the field returned by querying mysql through PDO to the String type

jacklove
Release: 2018-06-09 10:41:32
Original
2588 people have browsed it

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);?>
Copy after login

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:

Use php to obtain countries, provinces, cities, and surrounding data classes based on geographical coordinates

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template