请教高手关于mysql_data_seek分页的问题

zeoyeah 2010-07-26 03:47:10
初学PHP,在网上搜索了一下mysql_data_seek的用法,也大概知道了原理,但是实际做分页遇到一个坎,怎么也过不去。
我页面分页包括参数都已经做好了,就是显示记录的时候不灵,用下面这个语句出现的结果是:越到后面的页,记录数越少。
mysql_data_seek($result,($page-1)*$pagesize);

请教高手帮忙。谢谢。
...全文
70 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloyou0 2010-07-27
while循环里没看见对每页的记录数有控制?

BTW: 你这样做一样是每次都要查询数据库的,而且每次都是全部取出。。。。
所以还是用limit吧
回复
zeoyeah 2010-07-27
$result是当前分页的数据记录
$result1是总记录数用于计算页数
回复
zeoyeah 2010-07-27
130行:mysql_data_seek($result,($page-1)*$pagesize);
回复
zeoyeah 2010-07-27
我已经加多一个SQL语句查总记录数,但现在只要分页就有一个报错:
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
回复
zeoyeah 2010-07-26
<table id="table_content" border="0" cellspacing="0" cellpadding="0" align="center">
<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>
回复
zeoyeah 2010-07-26
没有方法把记录数查出来以后,把$result 分段显示?
回复
tbraz 2010-07-26
路过学习了……
回复
zeoyeah 2010-07-26
对,因为我的SQL语句比较复杂,所以就一次性把记录全查出来了。
回复
Dleno 2010-07-26
每次分页都要查询数据库。

select * from tablename where **** order by ** limit ($page-1)*$pagesize,$pagesize;
这只会查询出当前的记录,而不会都查出来。
你似乎把所有数据都查出来了
回复
zeoyeah 2010-07-26
但是不是经常要查询数据库?
回复
Dleno 2010-07-26
分页应该是:
select * from tablename where **** order by ** limit ($page-1)*$pagesize,$pagesize;
回复
zeoyeah 2010-07-26
我没有采用改变数据库查询条件的方法
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告