分页有效,但它没有进入下一页

时间:2016-05-01 17:54:26

标签: php mysql pagination

我在我的网站上添加了分页,一切都很好,但每当我点击下一步它将转到同一页面url / dataload2.php?page = 0。这是我的PHP代码,我认为它在计算或循环中有问题,谢谢你的帮助。

编辑:添加了更多代码

    <?php

include 'conn.php';
$num_rec_per_page=2;

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

$sql = "SELECT * from crewlist ORDER BY name ASC LIMIT $start_from, $num_rec_per_page";

$result = $conn->query($sql);

?>

 <?php 
    $sql = "SELECT * FROM crewlist"; 
    $rs_result = mysql_query($sql); //run the query
    $total_records = mysql_num_rows($rs_result);  //count number of records
    $total_pages = ceil($total_records / $num_rec_per_page); 

    echo "<a href='dataload2.php?page=1'>".'|<'."</a> "; // Goto 1st page  

    for ($i=1; $i<=$total_pages; $i++) { 
                echo "<a href='dataload2.php?page=".$i."'>".$i."</a> "; 
    }; 
    echo "<a href='dataload2.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
    ?>

编辑:更新语法

 <?php 
$sql = "SELECT * FROM crewlist"; 
$rs_result = mysqli_query($sql); //run the query
$total_records = mysqli_num_rows($rs_result);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='dataload2.php?page=1'>".'|<'."</a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='dataload2.php?page=".$i."'>".$i."</a> "; 
}; 
?>
<?php
echo "<a href='dataload2.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>

1 个答案:

答案 0 :(得分:2)

根据你的编辑:

$rs_result = mysqli_query($sql);
  • mysqli_query()要求将数据库连接作为第一个参数
  • 传递

将您的代码更改为:

$rs_result = mysqli_query($conn, $sql);

你会很高兴。

参考:

另外,请使用prepared statements阅读PDOprepared statements,因为SQL injection可能会遇到此问题。

  • 最好安全; - )

其他参考资料: