PHP+Mysql development of paging: writing page numbers to obtain data
<?php /**1,傳入頁碼**/ $page = $_GET["p"]; /**2,根據(jù)頁碼取出數(shù)據(jù):php->mysql的處理**/ $host = "localhost"; $username = "root"; $password = "123456789"; $db = "bbs2"; //連接數(shù)據(jù)庫 $conn = mysql_connect($host,$username,$password); if (!$conn) { echo "數(shù)據(jù)庫連接失敗"; exit; } //選擇要操作的數(shù)據(jù)庫 mysql_select_db($db); //設(shè)置數(shù)據(jù)庫編碼格式 mysql_query("SET NAMES UTF8"); //編寫sql獲取分頁數(shù)據(jù)SELECT * FROM 表名 LIMIT 起始位置,顯示條數(shù) $sql = "select * from test limit ".($page-1) * 5 .",5 "; //把sql語句傳送到數(shù)據(jù)中 $result = mysql_query($sql); //處理數(shù)據(jù) 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'; //連接數(shù)據(jù)庫 $conn = mysql_connect($host, $username, $password); if(!$conn){ echo "數(shù)據(jù)庫連接失敗"; exit;
Configure the database login file and connect to the database.
//選擇所要操作的數(shù)據(jù)庫 mysql_select_db($db); //設(shè)置數(shù)據(jù)庫編碼格式 mysql_query('SET NAMES UTF8'); //編寫sql獲取分頁數(shù)據(jù):SELECT * FROM 表名 LIMIT 起始位置 , 顯示條數(shù) $sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize"; if(!$sql){ echo "取出不成功"; }; //把sql語句傳送到數(shù)據(jù)庫 $result = mysql_query($sql); //處理我們的數(shù)據(jù) 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>"; //釋放結(jié)果 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.