【发布时间】:2017-09-18 18:48:31
【问题描述】:
我尝试将分页脚本添加到我现有的 php sql 查询页面。
但添加脚本后,页面会继续加载,不会显示任何内容或错误。
我的代码如下:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
现在是从各个表中获取数据的sql查询代码...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
此查询与要从哪个表中检索的内容有关..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <a href="post.php?id=<?php echo $row20['id_msg'];?>" style="font-weight: normal;"><?php echo $row20["body"]; ?></a></h4>
<div class="blog-meta clearfix">
<small><a href="post.php?id=<?php echo $row20['id_msg'];?>"><?php echo $countstep1["total"]; ?> Replies</a></small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
<a href="post.php?id=<?php echo $row20['id_msg'];?>" class="readmore" title="">View the topic →</a>
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '<a href="step1.php?page=' . $page . '">' . $page . '</a> ';
}
?>
<?php }
} else {
echo "";
}
?>
请注意,数据库连接都已检查并且是正确的..
任何帮助表示赞赏..
【问题讨论】:
-
为什么要为每个 PHP 代码打开和关闭 PHP 标记,除非您正在组合 HTML 和 PHP,否则您不必这样做。另外我认为你可以
JOINsql2和sql20都不需要运行 2 个查询。 Ans you while loopwhile($row20 = mysqli_fetch_array($result20))看不到右括号。
标签: php mysqli pagination