Home >Backend Development >PHP Tutorial >Why Does PDO Return Strings Instead of Numbers from MySQL, and How Can I Fix It?
PDO: Retrieving Numeric MySQL Data
When fetching integer values from MySQL using PDO, it's common to encounter string representations of numbers rather than actual numeric data. To remedy this, you may consider using the PDO::ATTR_STRINGIFY_FETCHES attribute. However, this attribute is not applicable to the MySQL driver.
Instead, you can disable prepared statement emulation to enable the MySQL native driver to return native data types. This can be achieved by:
new PDO($dsn, $user, $pass, array( PDO::ATTR_EMULATE_PREPARES => false ))
While receiving strings for numeric values may seem unusual, it is a common occurrence due to the default behavior of MySQL drivers. By modifying the prepared statement emulation, you can ensure that numeric values are retrieved as expected.
The above is the detailed content of Why Does PDO Return Strings Instead of Numbers from MySQL, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!