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 限制的情况下获取找到的总行数?
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01