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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)