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 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答