問題描述
我最近開始使用 PHP5 開發一個新項目,并希望使用他們的 PDO 類.問題是 MySQL PDO 驅動程序不支持 rowCount(),因此無法運行查詢然后獲取受影響的行數或返回的行數,就我而言,這是一個非常大的問題.我想知道之前是否有其他人處理過這個問題,以及您為解決這個問題做了什么.必須執行 fetch() 或 fetchAll() 來檢查是否有任何行受到影響或返回對我來說似乎是一種黑客行為,我寧愿只執行 $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
查詢之后立即發出 SELECT FOUND_ROWS()
查詢以獲取行數.
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();
另見:關于 FOUND_ROWS 的 MySQL 文檔()
這篇關于PHP5 的 PDO rowCount MySQL 問題的解決方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!