Mysql returning all integers as string in PHP

Asked

Viewed 189 times

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 |
  • 1

    In PDO has a flag that forces the result of fields to always be strings.

  • @and has a flag that does the opposite?

  • 1

    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.

1 answer

5


When making the conversion to JSON with the json_encode, you can pass the parameter JSON_NUMERIC_CHECK, it is available from the version 5.3.3, it basically checks whether that value can be considered numerical(I believe with the function is_numeric), and, if so, present JSON as a.

See these examples:

Common use with numerical values:

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr, JSON_NUMERIC_CHECK); //{"a":1,"b":2,"c":3,"d":4,"e":5}

Use with string values that can be converted to numeric type.

$arr = array('a' => "1", 'b' => "2", 'c' => "3", 'd' => "4", 'e' => "5");
echo json_encode($arr, JSON_NUMERIC_CHECK); //{"a":1,"b":2,"c":3,"d":4,"e":5}

See more about the json_encode in official php documentation.

Using the native Mysql driver:

By default, all database/PHP operations will return all numeric values as a string to get around this in MySql, from PHP 5.3.0, you can use Mysql Native Driver, it is a PHP extension written as part of the project, where it is under the license of the same.

Using the mysqlnd and the mysqli, you can ask to return the numeric values with their correct types by passing the following option:

$mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);

Already using the PDO, it will automatically do the conversions, but all queries must be prepared before on the server-side.

You can see the comparison between the mysqlnd and the libmysqlclient here.

  • Great solution. But I wonder about the case of not using JSON and have to get the results as a whole. The idea is that you have a solution for the problem that you solve in the communication between PHP and MYSQL (tip: PHP mysql Native driver :D)

  • @Wallacemaxters Dude, any selection communication between php/bd will turn numerical values into string, because php is dynamic, it is generally not worth doing the conversion. You could use the mysqlnd, but maybe it is not worth the installation of a lib when the current problem is only to turn the data into JSON, this can be corrected at the time of parse.

  • No, no. JSON in question is just an example. The problem is actually MYSQL.

  • 1

    @Wallacemaxters You seem to know how does kk, anyway I added in my reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.