直接上马:
$page = isset($_GET['p'])? $_GET['p']:1; $page = ($page ==0)? 1 : $page; $y = 5; $x = ($page-1)*$y; $dsn = "mysql:host=localhost;dbname=demo;charset=utf8"; $pdo = new PDO($dsn,'root','root'); $sql = "SELECT * FROM `user1` LIMIT :x,:y "; $pdoStmt = $pdo->prepare($sql); $pdoStmt->bindParam('x',$x,PDO::PARAM_INT); $pdoStmt->bindParam('y',$y,PDO::PARAM_INT); $pdoStmt->execute(); echo '<h3 align="center">用户信息表</h3>'; echo '<table border="1" cellpadding="5" cellspacing="0" width="70%" align="center">'; echo '<tr bgcolor="#adff2f">'; echo '<th>ID</th><th>name</th><th>email</th>'; echo '</tr>'; foreach($pdoStmt as $row){ echo '<tr align="center">'; echo '<td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['email'].'</td>'; echo '</tr>'; } echo '</table>'; $sql = "SELECT * FROM `user1`"; $pdoStmt1 = $pdo->prepare($sql); $pdoStmt1->execute(); //echo $pdoStmt1->rowCount(); $pageC = ceil($pdoStmt1->rowCount()/$y); $page = ($pageC == $page)? ($page-1):$page; echo '<h3 align="center">'; echo '<a href="http://www.php.io/demo/29/4.52.php?p=1">首页</a>'; echo '<a href="http://www.php.io/demo/29/4.52.php?p='.($page-1).'">上一页</a>'; for($i=1;$i<=$pageC;$i++){ echo '<a href="http://www.php.io/demo/29/4.52.php?p='.$i.'">'.$i.'</a>'; } echo '<a href="http://www.php.io/demo/29/4.52.php?p='.($page+1).'">下一页</a>'; echo '<a href="http://www.php.io/demo/29/4.52.php?p='.$pageC.'">尾页</a>'; echo '</h3>';