如何在MYSQL中使用LIMIT和OFFSET进行分页?

时间:2014-04-23 09:19:26

标签: javascript php mysql

通过这段代码,我可以从数据库中检索前五条记录。

但是我需要在按下分页箭头后显示数据库中剩余的5行。我正在做一个像Youtube一样的视频共享网站。

  <?php
include "config.php";


$q = mysql_query("SELECT * FROM register r
                  JOIN videos v ON r.id = v.video_id
                  ORDER BY r.id LIMIT 5") or die (mysql_error());

$headers = $col = "";
$row=mysql_num_rows($q);
$s=null;
echo "<h1> Top Most Videos </h1>";
while ($row = mysql_fetch_array($q)) 
{
if($row['id']!=$s)
{
$s = $row['id'];

echo "<div class='property'>";
echo "<div class='property1' >";
echo "<a href='#'><video src=\"".$row['path']."\" height='100' width='170' style= margin:5px; controls='controls'></video></a>";
echo "</div>";
echo "<div class='property2'>";
echo $row['videoname'];
echo "</div>";
echo "<div class='property3'>";
echo "<br/>";
echo "by:";
echo $row['name'];
echo "<br/>";
echo "</div>";
echo "<div class='property4'>";
echo $row['views'].'&nbsp;&nbsp;views&nbsp;';
echo $row['time'].'&nbsp;&nbsp;year ago';
echo "</div>";
echo "</div>";
}
}
echo "<input type='button' class='btn1'>";
 ?>
 </div>

喜欢javascript,php,mysql

6 个答案:

答案 0 :(得分:2)

LIMIT 5从结果中获取前5行(结果集中的位置0,1,2,3和4)。

LIMIT 5,5从您的结果集中获取5行,但从第5位开始。因此,您将获得&#39; next&#39; 5个结果位于第5,6,7,8和9位。

示例: 如果没有LIMIT的结果如下:

-----------------
|  ID  |  video |
-------+---------
|  1   |  cat   |
-------+---------
|  2   |  dog   |
-------+---------
|  3   |  bird  |
-------+---------
|  4   |  cow   |
-------+---------
|  5   |  snake |
-------+---------
|  6   |  fish  |
-------+---------
|  7   |  mouse |
-------+---------
|  8   |  shark |
-------+---------
|  9   | seal   |
-------+---------
|  10  | rabbit |
-----------------

现在您使用LIMIT 5LIMIT 5, 0

-----------------
|  ID  |  video |
-------+---------
|  1   |  cat   |
-------+---------
|  2   |  dog   |
-------+---------
|  3   |  bird  |
-------+---------
|  4   |  cow   |
-------+---------
|  5   |  snake |
-------+---------

现在您使用LIMIT 5, 5

-----------------
|  ID  |  video |
-------+---------
|  6   |  fish  |
-------+---------
|  7   |  mouse |
-------+---------
|  8   |  shark |
-------+---------
|  9   | seal   |
-------+---------
|  10  | rabbit |
-----------------

答案 1 :(得分:1)

您可以在限制条款

中使用LIMIT和偏移量

限制5,5

答案 2 :(得分:0)

您可以使用DATABASE的LIMIT和OFFSET概念在应用程序中提供分页。或者甚至可以使用Jquery的DATATABLE插件来提供分页。

答案 3 :(得分:0)

  

试试这个你的剧本

<?php
$sql = "SELECT * FROM register r JOIN videos v ON r.id = v.video_id ORDER BY r.id desc";
$result = mysql_query($sql);
$no = mysql_num_rows($result);

if (isset($_GET['page'])) { 
    $page = preg_replace('#[^0-9]#i', '', $_GET['page']); 
} else { 
    $page = 1;
} 
$itemsPerPage = 5; 

$lastPage = ceil($no / $itemsPerPage);

if ($page < 1) { 
    $page = 1; 
} else if ($page > $lastPage) { 
    $page = $lastPage; 
} 

$centerPages = "";
$sub1 = $page - 1;
$sub2 = $page - 2;
$add1 = $page + 1;
$add2 = $page + 2;
if ($page == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($page == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
} else if ($page > 2 && $page < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($page > 1 && $page < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $page . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'limit ' .($page - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("SELECT * FROM register r JOIN videos v ON r.id = v.video_id ORDER BY r.id desc $limit"); 
$paginationDisplay = ""; 

if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $page . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';

    if ($page != 1) {
        $previous = $page - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $previous . '" style="text-decoration:none;"> Previous </a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($page != $lastPage) {
        $nextPage = $page + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $nextPage . '" style="text-decoration:none;"> Next</a> ';
    } 
}

echo "<h1> Top Most Videos </h1>";
while ($row = mysql_fetch_array($sql2)) 
{
    if($row['id']!= '')
    {
        $s = $row['id'];

        echo "<div class='property'>";
        echo "<div class='property1' >";
        echo "<a href='#'><video src=".$row['path']." height='100' width='170' style= margin:5px; controls='controls'></video></a>";
        echo "</div>";
        echo "<div class='property2'>";
        echo $row['videoname'];
        echo "</div>";
        echo "<div class='property3'>";
        echo "<br/>";
        echo "by:";
        echo $row['name'];
        echo "<br/>";
        echo "</div>";
        echo "<div class='property4'>";
        echo $row['views'].'&nbsp;&nbsp;views&nbsp;';
        echo $row['time'].'&nbsp;&nbsp;year ago';
        echo "</div>";
        echo "</div>";
    }
}
echo "<input type='button' class='btn1'>";
?>

答案 4 :(得分:0)

你必须让它变得动态。限制和偏移量不应该是静态值。

每次更改页码时都必须增加偏移量。

$page_number = $_GET['page']; // page number 2 
$limit = 5; // How many rows to display per page
$offset =  $limit * $page_number; // this displays rows from 10 - 15   

 $q = mysql_query("SELECT * FROM register r
                  JOIN videos v ON r.id = v.video_id
                  ORDER BY r.id LIMIT $limit OFFSET $offset") or die (mysql_error());

使用链接我们可以将页码发送到查询页面

<a href="?page=2">Next</a>

答案 5 :(得分:-2)

对于接下来的五条记录,您必须更改查询。获取最后一行的id并使用where子句编写查询,例如id&gt;&#39; id last last row&#39;。