php - 如何使用准备好的语句获取 SQL_CALC_FOUND_ROWS 值?

标签 php mysql mysqli prepared-statement sql-calc-found-rows

我目前正在摸索如何使用准备好的语句实现 SQL_CALC_FOUND_ROWS

我正在编写一个分页类,显然我想向查询添加 LIMIT,但也想知道总行数是多少。

这是相关类(class)的示例。

$query = "select SQL_CALC_FOUND_ROWS id,title,location,salary,employer from jobs where region=38 limit 0,3";

if($stmt = $connection->prepare($query)) {
    $stmt->execute()or die($connection->error); //execute query
    $stmt->bind_result($id,$title,$location,$salary,$employer,$image);
    while($stmt->fetch()){
        $jobs[$x]['id']=$id;
        $jobs[$x]['title']=$title;
        $jobs[$x]['location']=$location;
        $jobs[$x]['salary']=$salary;
        $jobs[$x]['employer']=$employer;
        $jobs[$x]['image']=$image;
        $x++;
    }
    $stmt->close();//close statement
}

我有点困惑如何获取 SQL_CALC_FOUND_ROWS 实际值?我曾想过添加类似的内容:

$stmt->store_result();
$count=$stmt->num_rows;

但这仅给出了基于 LIMIT 的数字,因此在上面的示例中是 3,而不是应有的完整 6。

最佳答案

设法弄清楚,我将在下面为任何对 future 感兴趣的人详细说明我的答案。

原始代码

$query="select SQL_CALC_FOUND_ROWS id,title,location,salary,employer from jobs where region=38 limit 0,3";

if($stmt = $connection->prepare($query)) {
        $stmt->execute()or die($connection->error); //execute query
        $stmt->bind_result($id,$title,$location,$salary,$employer,$image);
        while($stmt->fetch()){
            $jobs[$x]['id']=$id;
            $jobs[$x]['title']=$title;
            $jobs[$x]['location']=$location;
            $jobs[$x]['salary']=$salary;
            $jobs[$x]['employer']=$employer;
            $jobs[$x]['image']=$image;
            $x++;
        }
        $stmt->close();//close statement
    }

更新代码

$query="select SQL_CALC_FOUND_ROWS id,title,location,salary,employer from jobs where region=38 limit 0,3";

if($stmt = $connection->prepare($query)) {
        $stmt->execute()or die($connection->error); //execute query
        $stmt->bind_result($id,$title,$location,$salary,$employer,$image);
        while($stmt->fetch()){
            $jobs[$x]['id']=$id;
            $jobs[$x]['title']=$title;
            $jobs[$x]['location']=$location;
            $jobs[$x]['salary']=$salary;
            $jobs[$x]['employer']=$employer;
            $jobs[$x]['image']=$image;
            $x++;
        }
            //get total number of rows.
            $query="SELECT FOUND_ROWS()";
            $stmt = $connection->prepare($query);
            $stmt->execute();
            $stmt->bind_result($num);
            while($stmt->fetch()){
                $count=$num;
            }

        $stmt->close();//close statement
    }

也许可以用另一种方式做得更好,但似乎无法在网上找到任何好的例子,这很有效!

关于php - 如何使用准备好的语句获取 SQL_CALC_FOUND_ROWS 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13049725/

相关文章:

mysql - 在 MySQL 数据库的列上设置 'NOT NULL' 属性的查询应该是什么?

mysql - memcache 会帮助处理昂贵的 MySQL 查询吗? (Drupal 站点)

php - 为什么我得到 mysql_connect() : Lost connection to MySQL server and how can I fix it?

php - 计算每行 Union 和 Left Join 的评论

php - 如何调用validation.php中编写的函数并将所有函数名称存储在数据库中

php - 将变量添加到上一页的(指定文本字段)

php - 使用 PHP 查找 JSON URL 响应中的特定条目

php - 如果我使用 MySQLi 准备语句,是否需要转义我的变量?

php - Mysqli LIKE 无法使用撇号?

Android 设备上的 PHP HTTP POST 使用 HttpURLConnection