5
Whenever I am making a query, PHP is bringing the data from the MYSQL database as string
, and this is affecting data conversion when I send the customer a JSON response.
For example, in the table users
have the fields id
, name
and role_id
. When I return the JSON of this data, instead of getting:
{ "id" : 1, "name": "Wallace", "role_id" : 12 }
I’m getting:
{"id" : "1", "name": "Wallace", "role_id" : "12" }
That is, somehow the data returned from MYSQL is being treated all as string
.
I could do a CAST, but doing it all the time could be a pain in the ass.
How could I solve this problem?
Could be a problem with MYSQL or the PHP extension responsible for communicating with it?
See the structure of the table below:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`role_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
In PDO has a flag that forces the result of fields to always be strings.
– rray
@and has a flag that does the opposite?
– Wallace Maxters
In this case it is the
PDO::ATTR_STRINGIFY_FETCHES
take a look at the value. If you have the opposite I don’t remember head.– rray