How to get the total found rows without considering the limit in PDO?(如何在不考虑 PDO 限制的情况下获取找到的总行数?)
问题描述
我发现为了显示加载更多按钮而进行辅助查询以检查更多结果非常不专业.
我正在使用 PDO,有什么方法可以在没有限制过滤器的情况下获得找到的总行数,但仍然过滤结果?
当前代码:
//显示结果$开始= 0$r=$db->prepare("SELECT * FROM location WHERE area=:area LIMIT $start,10");$r->execute($fields);//查看是否有更多结果$r=$db->prepare("SELECT * FROM location WHERE area=:area");$r->execute($fields);$offset=$start+10;if($r->rowCount()>$offset){echo "加载更多";}
MySQL only AFAIK:
$r=$db->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM location WHERE area=:area LIMIT $start,10");$r->execute($fields);var_dump($r->fetchAll());var_dump($db->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN));
对于数据库服务器来说,当然与查询所有记录的时间一样繁重.对于非 MySQL 使用,此查询当然比获取所有记录的行数要好:
$r=$db->prepare("SELECT COUNT(*) FROM location WHERE area=:area");$r->execute($fields);$count = $r->fetch(PDO::FETCH_COLUMN);回声 $count;
I found it very unprofessional to make a secondary query to check for more results in order to display a load more button.
I'm using PDO, is there any way to get the total found rows without the limit filter but still filtering the results?
Current Code:
// Show Results
$start = 0
$r=$db->prepare("SELECT * FROM locations WHERE area=:area LIMIT $start,10");
$r->execute($fields);
// See if there is more results
$r=$db->prepare("SELECT * FROM locations WHERE area=:area");
$r->execute($fields);
$offset=$start+10;
if($r->rowCount() > $offset){
echo "<div class='load'>Load More</div>";
}
MySQL only AFAIK:
$r=$db->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM locations WHERE area=:area LIMIT $start,10");
$r->execute($fields);
var_dump($r->fetchAll());
var_dump($db->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN));
About as heavy for the database server as querying a single time for all the records of course. For non-MySQL use, this query is of course better then getting the rowcount of all the records:
$r=$db->prepare("SELECT COUNT(*) FROM locations WHERE area=:area");
$r->execute($fields);
$count = $r->fetch(PDO::FETCH_COLUMN);
echo $count;
这篇关于如何在不考虑 PDO 限制的情况下获取找到的总行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不考虑 PDO 限制的情况下获取找到的总行数?
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01