问题描述
我最近开始使用PHP5从事新项目的工作,并希望为其使用PDO类.问题是MySQL PDO驱动程序不支持rowCount(),因此无法运行查询然后获取受影响的行数或返回的行数,就我而言,这是一个很大的问题.我想知道以前是否有人处理过这个问题,以及您为解决这个问题做了什么.对我而言,不得不执行fetch()或fetchAll()来检查是否有任何行受到影响或返回了对我来说似乎是hack,我宁愿只执行$ 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();
这篇关于PHP5的PDO rowCount MySQL问题的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!