限制 php mysql 中的数据


Limiting Data in php mysql

我已经做了一本大满贯书,它仍在建设中。我写了以下代码:

<?php
$n=$_REQUEST['n'];
$n=strtolower($n);
echo "<html><body alink=black vlink=black link=black><center><h1>";
$target_dir = "fu/uploads/";
$target_file = $target_dir . $n . ".jpg";
if (file_exists($target_file)) {
echo "<img src='"http://slambook.esy.es/fu/uploads/".$n.".jpg'" width=85 height=85 alt='"Profile Pic'">";
}
echo "Welcome to your wall, ".$n."</h1><br><br>";
echo "<a href='"http://slambook.esy.es/passchangeh.php?n=".$n."'"><button>Change Password</button></a><br><br>";
echo "<form action='"visit.php'" method='"get'">";
echo "<input type='"hidden'" name='"u'" value='"".$n."'">";
echo "<input type='"text'" name='"n'" placeholder='"Search A Nickname'">";
echo "<input type=submit value='"Search By Nickname'"></form>";
echo "<form action='"sbi.php'" method='"post'">";
echo "<input type='"hidden'" name='"u'" value='"".$n."'">";
echo "<input type='"text'" name='"id'" placeholder='"Search A id'">";
echo "<input type=submit value='"Search By Id'"></form>";
$post="http://slambook.esy.es/post.php?n=".$n;
echo "<a href=".$post.">Fill Someone's Slambook</a><br><br><br>";
$servername = "myhost";
$username = "myusername";
$password = "mypass";
$dbname="mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT i,f,e,p,m,d FROM ".$n." ORDER BY i DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    echo "<center><table border=2><tr><th>Date</th><th>From</th><th>Email</th><th>Phone</th><th>Message</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $from=$row["f"];
        echo "<tr><td>".$row["d"]."</td><td><a href='"http://slambook.esy.es/visit.php?n=".$from."'">".$from."</a></td><td>".$row["e"]."</td><td> ".$row["p"]."</td><td>".$row["m"]."</td></tr>";
    }
    echo "</table></center>";
} else {
    echo "Your slambook has been filled by 0 people!";
}
$conn->close();
?>

但是在运行这面墙时.php,所有记录都打印在一页上。我想限制每页的数据并使用分页系统。我想要的是桌子的尽头,必须有一个"上一篇文章"链接,该链接将显示下一篇文章。但是我尝试了很多次,结果是这样的:

<?php
$n=$_REQUEST['n'];
$p=$_REQUEST['p'];
$n=strtolower($n);
if($p=="")
$p=2;
$ipp=2;
$os=($p-1)*$ipp;
echo "<html><body alink=black vlink=black link=black><center><h1>";
$target_dir = "fu/uploads/";
$target_file = $target_dir . $n . ".jpg";
if (file_exists($target_file)) {
echo "<img src='"http://slambook.esy.es/fu/uploads/".$n.".jpg'" width=85 height=85 alt='"Profile Pic'">";
}
echo "Welcome to your wall, ".$n."</h1><br><br>";
echo "<a href='"http://slambook.esy.es/passchangeh.php?n=".$n."'"><button>Change Password</button></a><br><br>";
echo "<form action='"visit.php'" method='"get'">";
echo "<input type='"hidden'" name='"u'" value='"".$n."'">";
echo "<input type='"text'" name='"n'" placeholder='"Search A Nickname'">";
echo "<input type=submit value='"Search By Nickname'"></form>";
echo "<form action='"sbi.php'" method='"post'">";
echo "<input type='"hidden'" name='"u'" value='"".$n."'">";
echo "<input type='"text'" name='"id'" placeholder='"Search A id'">";
echo "<input type=submit value='"Search By Id'"></form>";
$post="http://slambook.esy.es/post.php?n=".$n;
echo "<a href=".$post.">Fill Someone's Slambook</a><br><br><br>";
$servername = "myhost";
$username = "myuser";
$password = "mypass";
$dbname="mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT * FROM ".$n." ORDER BY id DESC LIMIT ".$os.",".$ipp."";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $page_count = (int)ceil($result / $ipp);
   // double check that request page is in range
   if($p > $page_count) {
        // error to user, maybe set page to 1
        $p = 1;
   }
    echo "<center><table border=2><tr><th>Date</th><th>From</th><th>Email</th><th>Phone</th><th>Message</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $from=$row["f"];
        echo "<tr><td>".$row["d"]."</td><td><a href='"http://slambook.esy.es/visit.php?n=".$from."'">".$from."</a></td><td>".$row["e"]."</td><td> ".$row["p"]."</td><td>".$row["m"]."</td></tr>";
    }
    echo "</table></center>";
    echo "<a href='"http://slambook.esy.es/betawall.php?n=".$n."&p=".($p+1)."'">Previous Posts</a>";
} else {
    echo "No more Posts";
}
$conn->close();
?>

但是在这种情况下,有 5 条记录,并且每页的记录设置为 2,因此只有两页可见,第 5 条记录(应该在第 3 页上)永远不会出现。我应该怎么做才能修复此错误?

您可以使用数据表进行更轻松的分页,请检查此内容,并告诉我是否适合您