初学PHP,在网上搜索了一下mysql_data_seek的用法,也大概知道了原理,但是实际做分页遇到一个坎,怎么也过不去。
我页面分页包括参数都已经做好了,就是显示记录的时候不灵,用下面这个语句出现的结果是:越到后面的页,记录数越少。
mysql_data_seek($result,($page-1)*$pagesize);请教高手帮忙。谢谢。
我页面分页包括参数都已经做好了,就是显示记录的时候不灵,用下面这个语句出现的结果是:越到后面的页,记录数越少。
mysql_data_seek($result,($page-1)*$pagesize);请教高手帮忙。谢谢。
select * from tablename where **** order by ** limit ($page-1)*$pagesize,$pagesize;
这只会查询出当前的记录,而不会都查出来。
你似乎把所有数据都查出来了
<tr>
<td width="220px" align="center" valign="top">
<?
include 'inc/product_list_left.php';
?>
</td>
<td width="10px"></td>
<td id="workarea" valign="top">
<table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="#FFFFFF" bordercolorlight="#cccccc">
<?
$pagesize=15;
if(empty($_GET['page'])){
$page=1;
}else{
$page=intval($_GET['page']);
}
$startrow=($page-1)*$pagesize;
if (strtoupper($_GET['key1'])=="" and strtoupper($_GET['key2'])=="" and strtoupper($_GET['key3'])=="" and strtoupper($_GET['key4'])==""){
$sql = "select product_info.product_id, product_info.product_name, product_info.input, product_info.output, product_info.category, product_info.category0, product_info.category1, product_info.category2, product_info.category3, product_info.image_path, product_attachment.path from product_info,product_attachment where product_info.product_id like product_attachment.product_id order by rand() limit 15";
}else{
if (strtoupper($_GET['key1'])<>""){
$sql = "select product_info.product_id, product_info.product_name, product_info.input, product_info.output, product_info.category, product_info.category0, product_info.category1, product_info.category2, product_info.category3, product_info.image_path, product_attachment.path from product_info,product_attachment where product_info.category0 like '".$_GET['key1']."' and product_info.product_id like product_attachment.product_id order by order_by desc";
if (strtoupper($_GET['key2'])<>"")
$sql = "select product_info.product_id, product_info.product_name, product_info.input, product_info.output, product_info.category, product_info.category0, product_info.category1, product_info.category2, product_info.category3, product_info.image_path, product_attachment.path from product_info,product_attachment where product_info.category1 like '".$_GET['key2']."' and product_info.product_id like product_attachment.product_id order by order_by desc";
if (strtoupper($_GET['key3'])<>"")
$sql = "select product_info.product_id, product_info.product_name, product_info.input, product_info.output, product_info.category, product_info.category0, product_info.category1, product_info.category2, product_info.category3, product_info.image_path, product_attachment.path from product_info,product_attachment where product_info.category like '".$_GET['key3']."' and product_info.product_id like product_attachment.product_id order by order_by desc";
if (strtoupper($_GET['key4'])<>"")
$sql = "select product_info.product_id, product_info.product_name, product_info.input, product_info.output, product_info.category, product_info.category0, product_info.category1, product_info.category2, product_info.category3, product_info.image_path, product_attachment.path from product_info,product_attachment where product_info.category2 like '".$_GET['key4']."' and product_info.product_id like product_attachment.product_id order by order_by desc";
}
}
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);//mysql连线
$result=mysql_db_query($mysql_database,$sql,$conn);//对指定的资料库进行查询
$row=mysql_fetch_row($result);
$totalnum=mysql_num_rows($result);
$totalpage=ceil($totalnum/$pagesize);
//页码计算:
$lastpg=$totalpage; //最后页,也是总页数
$page=min($lastpg,$page);
if($page-1<=0){
$prepg=1;
}else{
$prepg=$page-1;
} //上一页
$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
$firstcount=($page-1)*$pagesize;
$url=$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
$parse_url=parse_url($url);
$url_query=$parse_url["query"];
if($url_query){
//因为URL中可能包含了页码信息,我们要把它去掉,以便加入新的页码信息。
//这里用到了正则表达式
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
//将处理后的URL的查询字串替换原来的URL的查询字串:
$url=str_replace($parse_url["query"],$url_query,$url);
}
echo "<tr><td colspan=5>From: ".($totalnum?($firstcount+1):0)." to ".min($firstcount+$pagesize,$totalnum).", Total:$totalnum.</td></tr>";
?>
<tr align="center" bgcolor="#eeeeee" class="ibm-price">
<td><span class="inleft1">Series</span></td>
<td><span class="inleft1">Input Voltage </span></td>
<td><span class="inleft1">Output voltage </span></td>
<td><span class="inleft1">File</span></td>
<td><span class="inleft1">Picture</span></td>
</tr>
<?
if ($row)
{
//将资料移动回第一笔
mysql_data_seek($result,($page-1)*$pagesize);
while($row=mysql_fetch_row($result))
{
?>
<tr>
<td><? echo($row['1']); ?> </td>
<td><? echo($row['2']); ?> </td>
<td><? echo($row['3']); ?> </td>
<td align="center"><a href="images<? echo($row['10']); ?>" target="_blank">View</a></td>
<td align="center"><img src="images<? echo($row['9']); ?>" width="120"></td>
</tr>
<?
}
}
else
{
echo("<tr><td colspan=5>No visible Product.</td></tr>");
}
?>
</table>
<div style="text-align:right; padding-right:20px; " class="pagination">
<?
if($lastpg >1){
if($page!=1){
echo("<a href=http://".$url."&page=".$prepg." class=prev>< Previous</a>");
}else{
echo("<span class=invalid>< Previous</span>");
}
for($i=1;$i<=$totalpage;$i++){
if($i==$page)
echo ("<span class=thispage>".$i."</span>");
else
echo ("<a href=http://".$url."&page=".$i." class=begin>".$i."</a>");
}
if($page==$totalpage){
echo("<span class=invalid>Next ></span>");
}else{
echo("<a href=http://".$url."&page=".$nextpg." class=next>Next ></a>");
}
}
?>
</div>
<p> </p>
</td>
</tr></table>
所以还是用limit吧
Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 60 is invalid for MySQL result index 64 (or the query data is unbuffered) in D:\php\website\Web_en\product_list.php on line 130
$result1是总记录数用于计算页数