19

I'm migrating my PHP codes from mysql (deprecated in 5.5) to PDO_MySQL. However, mysql_fetch_row returns integer while PDOStatement::fetch returns strings for numbers. How can I make PDO behaves like the former one?

Result of mysql_fetch_row:

array(1) {
  ["id"]=>
  int(1)
}

Result of PDOStatement::fetch:

array(1) {
  ["id"]=>
  string(1) "1"
}

3 Answers 3

14

Set PDO::ATTR_EMULATE_PREPARES to false, if you really need it with loosely-typed PHP

If mysql_fetch_row returns you int for SUM either (I never cared to check) - then it does some magic like if (ctype_digit($val)) $row[$key] = (int)$val; - so you can do in your DBAL

As far as I understand the way prepared statements works, it uses the same packet structure for either sending and retrieving data, and this packet contains data type.

It looks like that server can return data in 2 formats - native and mysqlnd, depends on the request type. A latter one can be interpreted by client library to cast resulting value.

Sign up to request clarification or add additional context in comments.

6 Comments

It works in most cases, but doesn't work for SELECT SUM(.... Also, as its name indicates, will advantages of prepared statements be invalid with this option?
To whoever reads this answer in the future: 1) Nope, he didn't misread the question. 2) Yes, it actually changes the data type of returned values.
@Haocheng Read this sentence carefully, it's double negative ;)
@YourCommonSense Thanks, and I'm curious about why this option has effect on this behavior, and I think it's undocumented in php.net.
I've checked that mysql returns string on SUM(.. as well.
|
14

For the records, PDO provides a feature that would alter this, PDO::ATTR_STRINGIFY_FETCHES:

Convert numeric values to strings when fetching. Requires bool.

The rationale behind the setting is that database engines can handle very large numbers (Oracle, for instance, allows 38-digit numbers) and PHP cannot. Retrieving those numbers as strings is a method to keep safe and prevent data loss.

Unluckily, the MySQL driver does not support it:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$sql = 'SELECT 1 AS integer_number, 3.14 AS floating_point';
$res = $pdo->query($sql);
while($row = $res->fetch(PDO::FETCH_ASSOC)){
    var_dump($row);
}

 

array(2) {
  ["integer_number"]=>
  string(1) "1"
  ["floating_point"]=>
  string(4) "3.14"
}

2 Comments

I like the rationale part of the answer, though if I have seen correctly in-use, the MySQL PDO doesn't support PDO::ATTR_STRINGIFY_FETCHES, as everything is returned as a string data-type already, meaning this attribute is fairly useless with that driver.
@Jon - You are right, I've just found it myself. I've updated my answer.
5

You can use json_encode and json_decode with JSON_NUMERIC_CHECK option to retrieve your array with numeric types:

$my_array = json_decode(json_encode( $my_array, JSON_NUMERIC_CHECK ));

1 Comment

The issue with JSON_NUMERIC_CHECK is that other text could be numeric, e.g. user-generated content. Seemingly random fields becoming numbers might be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.