为我的Html/Php/MySql脚本分页

为我的Html/Php/MySql脚本分页,php,html,mysql,pagination,Php,Html,Mysql,Pagination,如何对该脚本进行分页,使其在一页上显示5行,在另一页上显示5行,依此类推。我已经尝试了教程和更多的这一点,但仍然无法得到它 以下是我的代码: <?php require "manybr.htm" ?> <style> <?php require "styles.css" ?> </style> <?php $host="XXXXX"; // Host name $username="XXXX"; // Mysql username $p

如何对该脚本进行分页,使其在一页上显示5行,在另一页上显示5行,依此类推。我已经尝试了教程和更多的这一点,但仍然无法得到它

以下是我的代码:

<?php require "manybr.htm" ?>
<style>
<?php require "styles.css" ?>
</style>
<?php

$host="XXXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="tylted"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql 
$sql="SELECT * FROM $tbl_name order by id desc";
$result=mysql_query($sql);
?>
<table background='images/view.png' width='50%' align='center'>
<tr>
<th align='center'>Group</th><th align='center'>Submition By</th><th align='center'>Submition On</th><th align='center'>ScreenName</th><th     align='center'>Password</th><th align='center'>Does This Work?</th><th align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><b><a href="http://aol.cellufun.com/p/grp/grp.asp?v=??&grp=<? echo $rows['group']; ?>">{<? echo $rows    ['group']; ?>}</a> </b></td>
<td background='transparent' align='center'><b><a href="http://aol.cellufun.com/p/player.asp?v=&p=<? echo $rows['yname']; ?>"><? echo $rows['yname'];     ?><a> </b></td>
<td background='transparent' align='center'><b><? echo $rows['date']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['username']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['password']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['works']; ?>% Yes <font color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>%     No</b></td>
<td background='transpatent' align='center'><b><a href='works.php?id=<? echo $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30'     width='30'></a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img src='images/ThumbsDown.png' height='30' width='30'></a>

</td> 
</tr>

<?php
// close while loop 
}
?>

<?php
// close connection; 
mysql_close();
?>
</table>

^^^^更新上面的bozdoz

您可以在SQL查询中将限制设置为SQL查询追加限制5偏移量1以在第一页显示5个结果。增加下一页的偏移量值

例如:

偏移量2给出接下来的5个结果,以此类推

要将链接放置到下一页,可以尝试将偏移值设置为变量

OFFSET$OFFSET,其中$OFFSET在php中递增

index.php

然后是链接

<a href='index.php?p=2'>2</a>

应该显示2页,以此类推,您可以使用$GET变量来获取限制:

让URL类似于index.php?p=5

PHP可以包含$\u GET变量,如下所示:

<?php

$startnum = 0; //set default
if(isset($_GET['p'])&&is_numeric($_GET['p'])){
  $startnum = $_GET['p'];
}
$sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5";

?>
下一个和上一个链接可以是

这里是完整的PHP:
正如其他人已经说过的,您可以使用

为了得到这个偏移量,您可以从URL中获取一个可以命名页面的值,并将该变量放入到下一页和上一页的链接中

例如,假设页面上显示了五个项目。在第一页上,获取5行,以及所有项目的计数。如果还有更多项目,请添加到results.php?page=2的链接。从这个变量,我们知道我们的偏移量是多少。由于第一页的偏移量隐式为零,第二页将为5,第三页将为10,因此我们可以提取一个简单的公式:偏移量=$\u GET[page]-1*items\u每页

还有一些细节需要讨论,比如空结果集以及页面变量是否存在,但我将留给您来决定

参考:
如果我设置了一个限制,比如说5是它,我将如何包含一个指向第二个页面的链接,还有5个?如果我添加SELECT*FROM tylted limit 0,5到它,我将如何链接其他页面?使用$\u GET变量从URL获取页码。如果页面为home.html?page=5,则限制可以是limit$_GET['page'],5即限制5,从第5行开始。请停止编辑我的QCollaborative editing是一项功能。请查看网站常见问题解答。-请努力使用正确的拼写、语法、大写和标点符号,你会发现其他人需要较少地编辑你的文章。在这个例子中,你必须设置默认的$offset。您还应该检查$\u GET['p']是否设置了ifisset$\u GET['p']&&is\u int$\u GET['p']{…}@Charles他们正在编辑以缩短我所需的时间我感谢他们,但后来人们又把它改了回来,因为他对php完全陌生老实说,我有这个脚本已经3周了,看起来像我想要的,设置像我想要的,我从编辑中学到了更多,但现在我不知道你说了什么:ci增加了限制0,5到它,它现在显示5个结果现在我如何做链接来显示其他5个结果?//从mysql中选择record$offset=1$sql=从$tbl_名称顺序中选择*按id描述限制5偏移量。$OFFSET$结果=mysql\u查询$sql;?>警告:mysql_fetch_数组:对脚本执行此操作后,第53行的/home/a7972613/public_html/vp.htm中提供的参数不是有效的mysql结果资源//从mysql选择记录$offset=$\u GET['p']$sql=从$tbl_名称顺序中选择*按id描述限制5偏移量。$OFFSET$结果=mysql\u查询$sql;?>我点击了2,它加载了页面OK,所以我将每页的限制改为2,并将其改为p=1,p=2和p=3,它工作正常,但如果我只是去url而没有?p=1,它会给我这个错误,我还必须手动在url中键入它,它不会根据行数和限制自动生成链接,我用这一行替换了旧的链接,但是页面仍然没有改变页面url,但它显示了相同的帖子,这里的代码是://select record from mysql$startnum=0//设置默认IFAsset$_GET['p']&&is_int$_GET['p']{$startnum=$_GET['p'];}$sql=SELECT*FROM$tbl_name order by id desc LIMIT$startnum,5$结果=mysql\u查询$sql;?>警告您的代码示例包含一个-您正在将原始、未过滤、未验证的用户输入直接传递到SQL字符串中。请更正您的示例以删除该漏洞。Charles,什么漏洞?@Charles,什么漏洞?@bozdoz,哦,我完全没有注意到。我期待的是演员阵容,而不是验证检查。没有关系。老兄,我不是机器人*嘟嘟声*
<a href='index.php?p=2'>2</a>
<?php

$startnum = 0; //set default
if(isset($_GET['p'])&&is_numeric($_GET['p'])){
  $startnum = $_GET['p'];
}
$sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5";

?>
<style>
<?php require "styles.css" ?>
</style>
<?php

$host="XXXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="tylted"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$startnum = 0; //set default
if(isset($_GET['p'])&&is_numeric($_GET['p'])){
  $startnum = $_GET['p']; //change offset
}

$sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5";
$result=mysql_query($sql);
?>


See the <a href="?p=<?php echo $startnum+5; ?>">next page</a>
<table background='images/subbg.png' width='70%' align='center'>

<tr><th><?php require "links.php" ?></th></tr>
<tr>
<th align='center'>Group</th><th align='center'>Submition By</th><th    align='center'>Submition On</th><th align='center'>ScreenName</th><th   align='center'>Password</th><th align='center'>Does This Work?</th><th   align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><b><a     href="http://aol.cellufun.com/p/player.asp?v=&p=<? echo $rows['yname']; ?>"><? echo   $rows['yname']; ?><a> </b></td>
<td background='transparent' align='center'><b><? echo $rows['date']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['username']; ?></b></td>
 <td background='transparent' align='center'><b><? echo $rows['password']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['works']; ?>% Yes <font    color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>% No</b></td>
<td background='transpatent' align='center'><b><a href='works.php?id=<? echo   $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30' width='30'>  </a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img   src='images/ThumbsDown.png' height='30' width='30'></a>

</td>
</tr>

<?php
// close while loop 
    }
?>

</table>