dragam0217 2014-07-22 12:20
浏览 53
已采纳

MySQL PDO LIMIT

I found myselfe lately in need of pagination therefore i dug up my old mysql_query script that have served its purpose. I tried to redone it so it fits for my current MySQL PDO class but one thing isnt right.

The problem is that to find out how many pages query will produce, i need to first conduct entire query without LIMIT'ation, than i have to conduct again the same query to be able to limit it, it feels like waste of resources and time, those queries are really long, take approx 20 different filters to do it, so i believe that i must be doing something wrong.

Is there a way to check using PDO how many rows are there in total in the same time as applying LIMIT into it?

For now this is what i have to do:

exec i.e. this query:

SELECT * FROM inventory WHERE
    season = ?,
    category = ?,
    code = ?,
    storage = ?,
    price > ?,
    price < ?,
    purchase_date > ?,
    purchase_date < ?,
    index_date > ?,
    index_date < ?,
    product_class = ?

and than i got access to afected rows so i can exec it again and append LIMIT:

SELECT * FROM inventory WHERE
    season = ?,
    category = ?,
    code = ?,
    storage = ?,
    price > ?,
    price < ?,
    purchase_date > ?,
    purchase_date < ?,
    index_date > ?,
    index_date < ?,
    product_class = ?
    LIMIT {$BEGIN}, {$END}

Thank you in advance for all the help :)

  • 写回答

1条回答 默认 最新

  • duanjue2576 2014-07-22 12:29
    关注

    Why wouldn't you store all the query's result into an array, and print only the 10 first, then [11-20]. (10 is the arbitrary number of results showed)

    Something like

     for($i = ($page - 1) * 10; $i <= ($page * 10); i++)
    {
      echo $result[i]['season'];
      // other things
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?