PHP/MySQL 分页

发布于 2024-09-06 18:36:53 字数 170 浏览 1 评论 0原文

我是 php 和 sql 的新手,我有一个关于如何实现 sql 查询的小问题,可以:

  • 以数据库中的 5 个条目为例,插入 它们位于第一页(1-5)
  • 而不是从同一数据库中获取接下来的 5 个条目并将它们插入到另一页(5-10)
    等等:)

谢谢)

I am new to php and sql, and i have one tiny question about how to realize sql query , that can:

  • Take for example 5 entries from DB, insert
    them on 1st page (1-5)
  • Than take next 5 entries from same DB and insert them on another page (5-10)
    and so on :)

Thank you )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉世弥音 2024-09-13 18:36:53
SELECT col FROM table LIMIT  0,5; -- First page, rows 1-5
SELECT col FROM table LIMIT  5,5; -- Second page, rows 6-10
SELECT col FROM table LIMIT 10,5; -- Third page, rows 11-15

请阅读 MySQL SELECT 帮助页面上的 LIMIT 部分。如果要显示可用的总行数,可以进行额外计数,或使用 ROW_COUNT 函数。

SELECT col FROM table LIMIT  0,5; -- First page, rows 1-5
SELECT col FROM table LIMIT  5,5; -- Second page, rows 6-10
SELECT col FROM table LIMIT 10,5; -- Third page, rows 11-15

Read the LIMIT section on the MySQL SELECT helppage. If you want to display the total number of rows available, you can either do an extra count, or use the ROW_COUNT function.

转瞬即逝 2024-09-13 18:36:53

有几种方法可以做到这一点,我认为最快的性能是以下一种:

$ItemsPerQuery = 5;
$CurrentPage = 0; //here you can add the current site through $_GET array or other ways, but don't forget to use mysql_real_escape_string(), because of mysql injection 

$Query = mysql_query("SELECT * FROM table LIMIT ".($ItemsPerQuery*$CurrentPage).",".$ItemsPerQuery);

while($row = mysql_fetch_assoc($query))
{
    echo $row['column_name'];
}

您应该使用的东西:

There are several ways to do this, I think the fastest performance is following one:

$ItemsPerQuery = 5;
$CurrentPage = 0; //here you can add the current site through $_GET array or other ways, but don't forget to use mysql_real_escape_string(), because of mysql injection 

$Query = mysql_query("SELECT * FROM table LIMIT ".($ItemsPerQuery*$CurrentPage).",".$ItemsPerQuery);

while($row = mysql_fetch_assoc($query))
{
    echo $row['column_name'];
}

Stuff you should use:

巾帼英雄 2024-09-13 18:36:53

如果您的查询没有返回大量结果,请考虑使用 Jquery 通过 Tablesorter 函数对其进行分页。它需要一个格式正确的 HTML 表和页面、排序等,如果您不希望,则不需要额外的查询变量。它比采用成熟的 PHP 分页路线要容易得多,而且对于用户来说速度也快得多。

我个人最喜欢的例子在这里: http://datatables.net/

它可能不完全满足您的需求,但它对于许多应用程序来说都很酷。如果您这样做,请不要忘记使用 thead 和 tbody 设置表格格式,以便它可以正确拾取表格元素。

If your query does not return a prohibitive amount of results, consider using Jquery to page it with a Tablesorter function. It takes a properly formatted HTML table and pages, sorts, etc on the fly...no need for additional query variables if you don't wish. It's SO much easier than going the full-blown PHP paging route, and is much faster for the user.

My personal favorite example is here: http://datatables.net/

It may not fully suit your needs, but it is cool for many applications. If you do it, just don't forget to format your table with thead and tbody so that it can properly pick up the table elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文