Home >Database >Mysql Tutorial >How Can I Retrieve MySQL Query Results in Native Data Types in PHP?
Retrieving MySQL Query Results in Native Data Types
Problem:
When using mysql_fetch_row() and mysql_result() to fetch MySQL query results, numerical values are returned as strings. How can you retrieve data in its datatype stored in the table instead?
Answer:
In PHP 5.2, directly retrieving data in native datatypes (i.e., anything other than strings) is not possible.
Solution in PHP 5.3:
With PHP 5.3 and the new mysqlnd driver, you can utilize Server-side Prepared Statements to retrieve data in native datatypes. For example, an INT column will be returned as an integer instead of a string.
Limitations:
This method is only available for prepared statements using mysqlnd, and it requires PHP 5.3 or later with mysqlnd support enabled during compilation.
Alternative Solution:
Consider using an ORM or mapping system on the PHP side to convert database results to PHP datatypes. However, this approach may hinder the use of type-sensitive operators like === and !== compared to native datatype retrieval.
The above is the detailed content of How Can I Retrieve MySQL Query Results in Native Data Types in PHP?. For more information, please follow other related articles on the PHP Chinese website!