Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
506 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...