如何在不考虑 PDO 限制的情况下获取找到的总行数?

时间:2023-04-08
本文介绍了如何在不考虑 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 限制的情况下获取找到的总行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句 下一篇:PDO MSSQL Server - 未找到驱动程序

相关文章