gpt4 book ai didi

php - 如何通过分页从我的 mysql 数据库表中每页只显示 5 条记录?

转载 作者:行者123 更新时间:2023-11-29 03:21:50 24 4
gpt4 key购买 nike

我想通过分页显示每页五条记录(mysql、php、html、css),直到显示所有记录,导航到页面必须像,Page : 1 2 3 4 5 6 7 7 8...最后

这是我的代码,用于查看 emp_master 表中的所有记录。我是 PHP 的新手,所以请编写一个易于理解的分页代码。我见过几个例子,但它们不起作用。

<?php
$con=mysqli_connect("localhost","user","password","dataplus");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM emp_master");

echo "<table border='1'>";

$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";

mysqli_close($con);

我想通过分页每页显示 5 条记录,直到显示所有记录,导航到页面必须是这样的,Page: 1 2 3 4 5 6 7 7 8... Last.

以下代码无效:

     $dbhost="localhost";
$dbuser="10053";
$dbpass="n6867242";
$database="0368";

$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('1005368');

/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}

$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT emp_id, emp_name, e_mail ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP MAIL : {$row['e_mail']} <br> ".
"--------------------------------<br>";
}

if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |";
echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
}

mysql_close($conn);

最佳答案

如果你只需要 PHP MySQL 代码,我通常使用类似下面的代码。

$page=max(intval($_GET['page']),1); // assuming there is a parameter 'page'
$itemsperpage = 5;
$total=100; // total results if you know it already otherwise use another query
$totalpages = max(ceil($total/$itemsperpage),1);
$query = "SELECT * FROM emp_master LIMIT ".(($page-1)*$itemsperpage).",".$itemsperpage; // this will return 5 items based on the page

关于php - 如何通过分页从我的 mysql 数据库表中每页只显示 5 条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42999475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com