How to Retrieve Integer and Numeric Data Types as Integers and Numerics in PHP from MySQL
When querying MySQL using PHP, it's common to encounter an issue where integer and numeric columns are returned as string types. Despite configuring "PDO::ATTR_STRINGIFY_FETCHES" to false, this problem persists.
Solutions
Configure PDO:
Ensure that the following PDO attributes are set:
Returned Types
When using the mysqlnd driver, integer and numeric column values are returned as the following PHP data types:
Example
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $stmt = $pdo->prepare('SELECT * FROM table'); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_OBJ); echo $result->integer_col; // int echo $result->double_col; // float echo $result->decimal_col; // string echo $result->bigint_col; // string (since value exceeds 64-bit signed int)
Output:
1 1.55 1.20 18446744073709551615
The above is the detailed content of How to Ensure Integer and Numeric Data Types Are Returned Correctly from MySQL in PHP?. For more information, please follow other related articles on the PHP Chinese website!