問題描述
所以我使用 PHP 的 PDO 作為我的數(shù)據(jù)庫(kù) goto 類已經(jīng)有一段時(shí)間了,不幸的是今天在客戶端服務(wù)器上調(diào)試了一段時(shí)間后(安裝了 PHP 5.2.6)我發(fā)現(xiàn) 這個(gè).我們嘗試升級(jí)到最新的穩(wěn)定版本 (5.2.9),但問題仍然存在.
有沒有人找到解決方法?
數(shù)據(jù)庫(kù)可以為您計(jì)算行數(shù)的唯一方法是運(yùn)行查詢并計(jì)算行數(shù).
mysql 擴(kuò)展默認(rèn)使用緩沖查詢模式,這會(huì)導(dǎo)致在控制權(quán)返回給 PHP 之前將整個(gè)數(shù)據(jù)集提取到內(nèi)存中,并開始處理行.
PDO 默認(rèn)使用無緩沖模式,這會(huì)降低頁(yè)面加載時(shí)間的延遲,這通常是您想要的.權(quán)衡是 rowCount() 在獲取整個(gè)數(shù)據(jù)集之前不會(huì)返回有效信息.
那么你是如何得到這個(gè)數(shù)量的?
簡(jiǎn)單:
$q = $db->query("SELECT ...");$rows = $q->fetchAll();$rowCount = count($rows);echo "有 $rowCount 行
";foreach ($rows 作為 $row) {打印_r($row);}
<塊引用>
但這很糟糕,因?yàn)樗鼤?huì)在前面查詢所有行并使我的頁(yè)面加載速度變慢,舊的 mysql 擴(kuò)展沒有這個(gè)問題!?
但這正是舊的 mysql 擴(kuò)展在幕后實(shí)際做的事情;這是獲得該計(jì)數(shù)的唯一方法.
So I've been using PHP's PDO as my database goto class for a while now, unfortunately today after debugging for a while on a client's server (with PHP 5.2.6 installed) I discover this. We tried upgrading to the newest stable release (5.2.9) but the problem persists.
Has anyone found a workaround?
The only way that databases can give you a count for the number of rows is by running the query and counting the number of rows.
The mysql extension uses a buffered query mode by default that causes the entire dataset to be fetched into memory before control is returned to PHP and it can start to process the rows.
PDO uses an unbuffered mode by default which leads to lower latency in the page load time and is generally what you want. The trade off is that rowCount() won't return valid information until the entire dataset has been fetched.
So how do you get that count?
Easy:
$q = $db->query("SELECT ...");
$rows = $q->fetchAll();
$rowCount = count($rows);
echo "There are $rowCount rows
";
foreach ($rows as $row) {
print_r($row);
}
But that sucks because it queries all the rows up front and makes my page load slower, the old mysql extension didn't have this problem!?
But that's exactly what the old mysql extension is actually doing under the covers; it's the only way to get that count.
這篇關(guān)于PDO 的 rowCount() 不適用于 PHP 5.2.6+的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!