将row_number与prepare语句一起使用

问题描述 投票:0回答:2

我正在制作一个表,其中包含30个条目,来自MySQL的表。我在发送到sql的代码中使用LIMIT 30,但也有一堆下一页。该表将具有姓氏,名字和ID。在页面底部,有一个下一页按钮。它链接到同一页面,但在get变量中发送最后一个id。

<a href=<?php echo "students_view.php?id=$lastvari"; ?>><button type="button">Next Page</button></a>

$ lastvari是最后一个id。从mysql调用数据时,我正在使用prepare语句:

$stmt = $con->prepare("SELECT Last_Name, First_Name, Student_ID FROM Students LIMIT 30");
$stmt->execute();
$stmt->bind_result($last_name, $first_name, $student_id);

在发送给它之后,我如何告诉MySQL启动?我研究row_number(),但我不确定这是否是我正在寻找的。谢谢大家:)

php mysql prepared-statement
2个回答
0
投票

要获取第二页,请使用the two-number form of LIMIT

SELECT Last_Name, First_Name, Student_ID FROM Students LIMIT 30,30

要获得第三页:

SELECT Last_Name, First_Name, Student_ID FROM Students LIMIT 60,30

等等。第一页是LIMIT 0,30


0
投票

我建议另一种选择,而不是每次都将Last id传递给下一页。 假设当前网址为www.yoursite / students_view.php?page = 0

<?php
$start = $_GET['page'] * 30;
$stmt = $con->prepare("SELECT Last_Name, First_Name, Student_ID FROM Students LIMIT $start, 30");
$stmt->execute();
$stmt->bind_result($last_name, $first_name, $student_id);
?>

<?php
$page = $_GET['page']; //don't forget to sanitize
$page = $page+1;
?>
<a href="<?php echo "students_view.php?page=$page"; ?>" >
   <button type="button">Next Page</button>
</a>
© www.soinside.com 2019 - 2024. All rights reserved.