gpt4 book ai didi

php - 分页有效,但不会转到下一页

转载 作者:行者123 更新时间:2023-11-29 06:13:51 25 4
gpt4 key购买 nike

我在我的网站上添加了分页,一切都很好,但每当我点击下一步时,它都会转到同一页面 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
?>

最佳答案

根据您的修改:

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

将您的代码更改为:

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

一切顺利。

引用:

另外,请继续阅读 prepared statements , 或 PDOprepared statements因为你可能会被 SQL injection 击中.

  • 最好安全起见;-)

其他引用资料:

关于php - 分页有效,但不会转到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com