問(wèn)題描述
我正在將我的 PHP 代碼從 mysql
(在 5.5 中棄用)遷移到 PDO_MySQL
.但是,mysql_fetch_row
返回整數(shù),而 PDOStatement::fetch
返回?cái)?shù)字字符串.我怎樣才能讓 PDO 表現(xiàn)得像前者一樣?
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?
mysql_fetch_row
的結(jié)果:
array(1) {
["id"]=>
int(1)
}
PDOStatement::fetch
的結(jié)果:
array(1) {
["id"]=>
string(1) "1"
}
推薦答案
將 PDO::ATTR_EMULATE_PREPARES
設(shè)置為 false,如果您確實(shí)需要使用松散類型的 PHP
Set PDO::ATTR_EMULATE_PREPARES
to false, if you really need it with loosely-typed PHP
如果 mysql_fetch_row
為 SUM 返回 int 或者(我從不關(guān)心檢查) - 那么它會(huì)做一些魔術(shù),比如 if (ctype_digit($val)) $row[$key] =(int)$val;
- 所以你可以在你的 DBAL 中做
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
據(jù)我了解準(zhǔn)備語(yǔ)句的工作方式,它使用相同的數(shù)據(jù)包結(jié)構(gòu)來(lái)發(fā)送和檢索數(shù)據(jù),并且該數(shù)據(jù)包包含數(shù)據(jù)類型.
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.
看起來(lái)那個(gè)服務(wù)器可以返回兩種格式的數(shù)據(jù)——native 和 mysqlnd,這取決于請(qǐng)求類型.后一個(gè)可以被客戶端庫(kù)解釋為轉(zhuǎn)換結(jié)果值.
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.
這篇關(guān)于為什么 PDO_MySQL 不返回整數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!