限制php mysql中的数据

时间:2015-03-14 13:29:54

标签: php mysql limit

我做了一个slambook,它仍在建设中。 我写了以下代码:

<?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();
?>

但是在运行此wall.php时,所有记录都打印在一个页面上。 我想限制每页的数据并使用分页系统。 我想要的是在桌子的尽头, a&#34;之前的帖子&#34;链接必须在那里显示下一个帖子。 但我尝试了很多次,我发现的是这个:

<?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,因此只有两页可见,第五条记录(应该在第3页)永远不会出现。 我该怎么做才能解决这个错误?

1 个答案:

答案 0 :(得分:-1)

你可以使用Datatables来更轻松地分页,检查一下,并告诉我你是否可以为你工作