在MySQL上使用PDO进行分页 [英] Pagination using PDO with MySQL

查看:91
本文介绍了在MySQL上使用PDO进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的分页有一些问题.我直接在数据库控制台上执行了查询,并且工作正常.

I have some problems with the pagination. I executed the query directly on my database console and works fine..

public function method($arg, $db)//$db is a PDO connection link
{
    try
    {
        $next = $arg * 9;
        $top = 9;
        $sql = "SELECT col01, col02, col03 ";
        $sql .= "FROM table ";
        $sql .= "ORDER BY col01 ASC ";
        $sql .= ($next === 0)? "LIMIT ".$top : "LIMIT ".$next.", ".$top;    
        $return = $db->prepare($sql);
        $return->execute();

        $return->setFetchMode(PDO::FETCH_ASSOC);
        $this->minis = $return->fetch();
        return true;
    }
    catch(PDOExcepction $e)
    {
        return false;
    }
}

我在做什么错了?

推荐答案

您只返回第一行,因为您只调用了fetch()一次.循环调用它,并将结果累加到一个数组中:

You are only returning the first row because you only call fetch() once. Call it in a loop and accumulate the results into an array:

while ($row = $return->fetch()) {
  // Append the current row onto your array
  $this->minis[] = $row;
}
return true;

这篇关于在MySQL上使用PDO进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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