問(wèn)題描述
我最近開(kāi)始使用 PHP5 開(kāi)發(fā)一個(gè)新項(xiàng)目,并希望使用他們的 PDO 類.問(wèn)題是 MySQL PDO 驅(qū)動(dòng)程序不支持 rowCount(),因此無(wú)法運(yùn)行查詢?nèi)缓螳@取受影響的行數(shù)或返回的行數(shù),就我而言,這是一個(gè)非常大的問(wèn)題.我想知道之前是否有其他人處理過(guò)這個(gè)問(wèn)題,以及您為解決這個(gè)問(wèn)題做了什么.必須執(zhí)行 fetch() 或 fetchAll() 來(lái)檢查是否有任何行受到影響或返回對(duì)我來(lái)說(shuō)似乎是一種黑客行為,我寧愿只執(zhí)行 $stmt->numRows() 或類似的操作.
I've recently started work on a new project using PHP5 and want to use their PDO classes for it. The problem is that the MySQL PDO Driver doesn't support rowCount() so there's no way to run a query and then get the number of affected rows, or rows returned, which is a pretty big issue as far as I'm concerned. I was wondering if anyone else has dealt with this before and what you've done to work around it. Having to do a fetch() or fetchAll() to check if any rows were affected or returned seems like a hack to me, I'd rather just do $stmt->numRows() or something similar.
推薦答案
您可以在原始 SELECT
查詢之后立即發(fā)出 SELECT FOUND_ROWS()
查詢以獲取行數(shù).
You can issue a SELECT FOUND_ROWS()
query right after the original SELECT
query to get row count.
$pdo->query("SELECT * FROM users");
$foundRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();
另見(jiàn):關(guān)于 FOUND_ROWS 的 MySQL 文檔()
這篇關(guān)于PHP5 的 PDO rowCount MySQL 問(wèn)題的解決方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!