PHP-使用准备好的语句进行分页
我是新来的,所以对我放松点。我已经看过许多与这个主题相关的教程和问题。我正试图找出我做错了什么,因为我似乎无法用我准备好的语句进行分页。我的数据库当前有10行应该返回。我将尝试添加一个选项,以便稍后插入更多行,这样数字就会改变。 这是数据库查询和代码PHP-使用准备好的语句进行分页,php,Php,我是新来的,所以对我放松点。我已经看过许多与这个主题相关的教程和问题。我正试图找出我做错了什么,因为我似乎无法用我准备好的语句进行分页。我的数据库当前有10行应该返回。我将尝试添加一个选项,以便稍后插入更多行,这样数字就会改变。 这是数据库查询和代码 //determine how many results are available in database $sqlPosts = "SELECT count(post_owner)
//determine how many results are available in database
$sqlPosts = "SELECT
count(post_owner)
FROM post_table";
$stmt = $db -> prepare($sqlPosts);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($totalPosts);
$stmt->fetch();
//determine which page user is currently on
if (!isset($_GET['page']))
$current_Page = 1;
else
$current_Page = $_GET['page'];
// define how many results you want per page
$page = 1;
$rowsPerPage = 5;
//total number of available pages
$total_Pages = ceil($totalPosts/$rowsPerPage);
// determine the sql LIMIT starting number for the results on the displaying page
$start_from = ($page - 1) * $rowsPerPage;
// retrieve selected results from database and display them on page
$sql = "SELECT
body_of_post,
date_time,
post_owner,
title,
id
FROM
post_table
ORDER BY
date_time
DESC LIMIT
?,?";
$stmt = $db->prepare($sql);
$stmt->bind_param('dd', $start_from, $rowsPerPage);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($body_of_post, $date_time, $post_owner, $title, $id);
while ($stmt->fetch()) {
echo '<table border="1">
<tr>
<td bgcolor="#CCCCCC">Title</td>
<td bgcolor="#CCCCCC">Post</td>
<td bgcolor="#CCCCCC">Created by</td>
<td bgcolor="#CCCCCC">Date</td>
</tr>
<br>';
echo
'<tr><th><h2>"' . $title .'"</th>
<th>'. $body_of_post. '</th>
<th>'. $post_owner. '</th>
<th>'. $date_time. '</tr></th></h2>';
}
//确定数据库中有多少结果可用
$sqlPosts=“选择
计数(后所有者)
从post_表中”;
$stmt=$db->prepare($sqlPosts);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($totalPosts);
$stmt->fetch();
//确定用户当前所在的页面
如果(!isset($\u GET['page']))
$current_Page=1;
其他的
$current_Page=$_GET['Page'];
//定义每页需要多少个结果
$page=1;
$rowsPerPage=5;
//可用页面的总数
$total_Pages=ceil($totalPosts/$rowsPerPage);
//确定显示页面上结果的sql限制起始编号
$start_from=($page-1)*$rowsPerPage;
//从数据库检索所选结果并将其显示在第页上
$sql=“选择
邮政署署长,
日期和时间,
邮政署署长,
标题
身份证件
从…起
邮政表格
订购人
日期和时间
描述极限
?,?";
$stmt=$db->prepare($sql);
$stmt->bind_参数('dd',$start_from,$rowsPerPage);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($body_of_post,$date_time,$post_owner,$title,$id);
而($stmt->fetch()){
回声'
标题
邮递
创建人
日期
",;
回声
“.$title.”
“.$body_of_post”
“.$post_所有者。”
“.$date_time.”;
}
我想我的问题是在while循环中,但我不确定如何编写它。分页部分应该很好。当我单击下一个链接时,我看到index.php?page=将急剧更新,但数据库行保持不变。我希望page=1显示数据库中的前5行,而page=2显示接下来的5行,依此类推
这是分页部分
if($current_Page > 1){
$prev_page = $current_Page - 1;
$previous = "<a href=\"index.php?page=$prev_page\">Previous</a>";
}
if ($total_Pages > 1){
for($page = 1; $page <= $total_Pages; $page++){
if ($page == $current_Page){
$page_count = "";
}
else
echo "<a href=\"index.php?page=$page\">Next$rowsPerPage</a>";
}
echo $page_count;
}
if($current_Page>1){
$prev_page=$current_page-1;
$previous=“”;
}
如果($total_Pages>1){
对于($page=1;$page而言,问题在于以下行:
$start_from = ($page - 1) * $rowsPerPage;
将$page
变量设置为固定值1
,因此只显示第一组记录。将此行更改为
$start_from = ($current_Page - 1) * $rowsPerPage;
使用url中提供的页面
参数。您不能对限制
值使用参数。参数只能在SQL允许表达式的情况下使用,限制
后面必须跟文字,而不是表达式。@Barmar请尝试并查看