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