dscdttg4389 2014-04-17 20:06
浏览 148
已采纳

mysql LIMIT并不断检索行[重复]

This question already has an answer here:

I will do a project for my database class (new in databases). I will use php. The tables are created in mysql. I have to display a webpage with some information:

attr1 | atrr2 | atrr3 | atrr4 
      |       |       |
      |       |       |

But I want to display the last inserted 20 rows. Then, if the user wants to see the other rows inserted, the webpage will have a next page button showing the following 20 rows (not the same ones).

I know that I can use something like:

select (attributes) from tbl ORDER BY 'ID' DESC LIMIT 20

1- But then, How I can retrieve (with php programming) the other 20 rows? If I use the same query it will display the same information.

2- I was thinking to just query all the data and somehow store it, but I don't know if it is a good choice to store all the data even if the user does not want to see it. I want to do it as my first question because I have to retrieve other information from other tables in the database.

What do you suggest? And how I can retrieve the data as I want in my first question?

</div>
  • 写回答

2条回答 默认 最新

  • doujiang9887 2014-04-17 20:18
    关注

    I made one of these recently in a project. I used the Bootstrap pagination class to style it:

    <div class="pagination" style="text-align:center">
    <ul>
    
    <?php
    
            if($firstPage)
            {
                $relative = "./";
            }
            else
                $relative = "../";
    
            if($page != 1)
            {
                echo "<li><a href=\"".$relative."page/$page\">&laquo;</a></li>";
            }
            for($i = 1; $i <= ceil($numRows / $numItems); $i++)
            {
                if($i == $page)
                    echo "\t<li><a href=\"".$relative."page/$i"."\" style=\"background-color:#e5e5e5;color:#AAAAAA;\"><b>".$i."</b></a></li>";
                else
                    echo "\t<li><a href=\"".$relative."page/$i\">".$i."</a></li>";
            }
            if(($page+1) <= ceil($numRows / $numItems))
                echo "\t<li><a href=\"".$relative."page/".($page+1)."\">&raquo;</a></li>";
    ?>
    </ul>
    </div>
    

    Then in the controller, I form the query:

    $numItems = 20;  // Number of results to show per page
    $q = "SELECT * FROM --- WHERE ---- = $-----";
    $q .= " LIMIT ".(($page_number-1)*$numItems).','.($numItems*($page_number));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题