如何在不考虑PDO限制的情况下获得找到的总行数? [英] How to get the total found rows without considering the limit in PDO?

查看:61
本文介绍了如何在不考虑PDO限制的情况下获得找到的总行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现进行第二次查询以检查更多结果以显示加载更多按钮是非常不专业的.

I found it very unprofessional to make a secondary query to check for more results in order to display a load more button.

我正在使用PDO,有什么方法可以在没有限制过滤器的情况下获取找到的总行数,但仍在过滤结果?

I'm using PDO, is there any way to get the total found rows without the limit filter but still filtering the results?

当前代码:

// 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的AFAIK:

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));

对于数据库服务器而言,与查询一次所有记录当然一样繁重.对于非MySQL使用,此查询当然比获取 all 记录的行数更好:

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限制的情况下获得找到的总行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆