嗨我正在尝试为mysql查询创建分页,但它根本不起作用, 并在第一页显示在secand页面中的相同帖子,有人可以告诉我我的错误在哪里?到目前为止,这是我的代码。
<?php
// Connect to your database
$mysql_con = mysql_connect("localhost", "username", "password");
// Let's check if there were any errors
if(!$mysql_con){
// If there was ań error, if there was, let's display an error
die(mysql_error());
}
// Select your database
$mysql_db = mysql_select_db("mydatabase");
// If there were any errors with selecting your database, display an error
if(!$mysql_db){
die(mysql_error());
}
// include database connection
if(!isset($_GET['page']) || $_GET['page']==""){
$page = "1";
}else{
// If page is set, let's get it
$page = $_GET['page'];
}
if(!isset($_GET['c']) || $_GET['c']=="") {
$c="some value";
} else {
$c=$_GET['c'];
}
// Now lets get all messages from your database
$sql = "SELECT * FROM posts";
$query = mysql_query($sql);
// Lets count all messages
$num = mysql_num_rows($query);
// Lets set how many messages we want to display
$per_page = "5";
// Now we must calculate the last page
$last_page = ceil($num/$per_page);
// And set the first page
$first_page = "1";
// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";
// If page is 1 then remove link from "Previous" word
if($page == $first_page){
echo "Previous ";
}else{
if(!isset($page)){
echo "Previous ";
}else{
// But if page is set and it's not 1.. Lets add link to previous word to take us
back by one page
$previous = $page-1;
echo "<a href='?page=".$previous."'>Previous</a> ";
}
}
// If the page is last page.. lets remove "Next" link
if($page == $last_page){
echo "Next ";
}else{
// If page is not set or it is set and it's not the last page.. lets add link to this
word so we can go to the next page
if(!isset($page)){
$next = $first_page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}else{
$next = $page+1;
echo "<a href='?page=".$next."'>Next</a> ";
}
}
// And now lets add the "Last page" link
echo "<a href='?page=".$last_page."'>Last page</a>";
// Math.. It gets us the start number of message that will be displayed
$start = ($page-1)*$per_page;
// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";
// It's time for getting our messages
$sql = "SELECT * FROM posts $limit $start,$per_page";
$query = mysql_query($sql);
echo "<br /><br />";
// And lets display our posts
while($row = mysql_fetch_array($query) or die(mysql_error())){
echo $row['posts']."<br />";
}
?>