<?php /**1,传入页码**/ $page = $_GET["p"]; /**2,根据页码取出数据:php->mysql的处理**/ $host = "localhost"; $username = "root"; $password = "123456789"; $db = "bbs2"; //连接数据库 $conn = mysql_connect($host,$username,$password); if (!$conn) { echo "数据库连接失败"; exit; } //选择要操作的数据库 mysql_select_db($db); //设置数据库编码格式 mysql_query("SET NAMES UTF8"); //编写sql获取分页数据SELECT * FROM 表名 LIMIT 起始位置,显示条数 $sql = "select * from test limit ".($page-1) * 5 .",5 "; //把sql语句传送到数据中 $result = mysql_query($sql); //处理数据 echo "<table border=1 cellspacing=0 width=15%>"; echo "<tr><td>ID</td><td>名字</td><td>性别</td></tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td>{$row['sex']}</td>"; echo "<tr>"; } ?>
Because the default page number has not been added, to observe the effect, you need to add ?p=1 after the address for access.
Code explanation:
$page= $_GET['p']; where p is What is the function
Use the get method to pass it through the url, and p is used to pass the determined page number.
$host = "localhost"; $username = 'root'; $password = '123456789'; $db = 'bbs2'; //连接数据库 $conn = mysql_connect($host, $username, $password); if(!$conn){ echo "数据库连接失败"; exit;
Configure the database login file and connect to the database.
//选择所要操作的数据库 mysql_select_db($db); //设置数据库编码格式 mysql_query('SET NAMES UTF8'); //编写sql获取分页数据:SELECT * FROM 表名 LIMIT 起始位置 , 显示条数 $sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize"; if(!$sql){ echo "取出不成功"; }; //把sql语句传送到数据库 $result = mysql_query($sql); //处理我们的数据 echo "<table border=1 cellspacing=0 width=15%>"; echo "<tr><td>ID</td><td>名字</td><td>性别</td></tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td>{$row['sex']}</td>"; echo "<tr>"; } echo "</table>"; echo "</div>"; //释放结果 mysql_free_result($result);
$sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize";
SQL statement
"SELECT*FROM test LIMIT ".($page-1)*5 .",5 ";
(Current page number - 1) The number of data displayed on the page
Get the paging data from the database and display itAnd close the database to release the connection. Next Section