I did some testing today and couldn’t apparently get PHP to retrieve db fields that were integers (column type was INT) as integers. I could retrieve 1 from the database and compare it via ‘==’ to the integer 1 successfully, but not via ‘===’ (with type checking). Are all fields retrieved by PHP from a MySQL database automatically converted to strings? Are there exceptions?
Yes, PHP or more concisely the mysql c extension returns strings. It isn’t really a question of conversion but limitation c. In order to cast the data to the proper type would require separate queries for metadata of the table which would add a significant inefficiency to the process of php communicating with mysql.
I think though if you’re using pdo or mysqli it is possible to bind column values and have them casted. However, this still requires defining what the data type will be on the application side.
Good to know, and thanks for explaining why. That make sense. I do use PDO, so I will look into the bind column method you linked.
However, I made the observation that when you push data into a class (via PDO::FETCH_CLASS) you can get the proper type conversion.