gpt4 book ai didi

php - 如何在 PHP 中对 Mysql 表进行分页

转载 作者:可可西里 更新时间:2023-11-01 07:46:44 25 4
gpt4 key购买 nike

我在 mysql 中有一个表。因为它有很多行,所以我想将每 10 行放在一个页面中,然后通过单击链接显示接下来的 10 行。有什么解决办法吗?

最佳答案

真的很棒http://www.phpsimplicity.com/tips.php?id=1

就是这么简单!无需处理庞大的类(class)!我很高兴:D

<!DOCTYPE html>
<html lang="en">
<head>
<title>Paginate</title>
</head>
<body>
<form method='get'>
<?php
$connection = Mysql_connect( 'server', 'user', 'pass' );
if ( ! $connection ) {
echo 'connection is invalid';
} else {
Mysql_select_db( 'DB', $connection );

}
//Check if the starting row variable was passed in the URL or not
if ( ! isset( $_GET['startrow'] ) or ! is_numeric( $_GET['startrow'] ) ) {

//We give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;

//Otherwise we take the value from the URL
} else {
$startrow = (int) $_GET['startrow'];
}
//This part goes after the checking of the $_GET var
$fetch = mysql_query( "SELECT * FROM sample LIMIT $startrow, 10" ) or
die( mysql_error() );
$num = Mysql_num_rows( $fetch );
if ( $num > 0 ) {
echo '
<table border=2>';
echo '
<tr>
<td>ID</td>
<td>Drug</td>
<td>quantity</td>
</tr>
';
for ( $i = 0; $i < $num; $i ++ ) {
$row = mysql_fetch_row( $fetch );
echo '
<tr>';
echo "
<td>$row[0]</td>
";
echo "
<td>$row[1]</td>
";
echo "
<td>$row[2]</td>
";
echo '
</tr>
';
}//for
echo '
</table>
';
}

//Now this is the link..
echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . ( $startrow + 10 ) . '">Next</a>';

$prev = $startrow - 10;

//only print a "Previous" link if a "Next" was clicked
if ( $prev >= 0 ) {
echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . $prev . '">Previous</a>';
}
?>
</form>
</body>
</html>

顺便说一下rickyduck的链接也不错

关于php - 如何在 PHP 中对 Mysql 表进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6677580/

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