我在分页中使用函数mysql_data_seek()
时遇到了问题。单击链接时,它不会产生任何问题,但是当单击分页链接的最后一页时,会产生如下错误:
Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 14 is invalid for MySQL result index 6 (or the query data is unbuffered)
我的代码如下:
$rpp = 3;
$adjacents = 4;
$page = (!empty($_GET["page"]) ? intval($_GET["page"]) : 1);
if($page<=0) $page = 1;
$reload = $_SERVER['PHP_SELF'];
$sql = "SELECT * FROM ".TABLE_IMAGE." ORDER BY id ASC";
$qry = mysql_query($sql, $con);
$tcount = mysql_num_rows($qry);
$tpages = ($tcount) ? ceil($tcount/$rpp) : 1;
$i = 1;
$count = 0;
$j = ($page-1)*$rpp;
while(($result = mysql_fetch_array($qry)) && (($count<$rpp) && ($j<=$tcount))){ mysql_data_seek($qry,$j);
$id = $result['id'];
$img = $result['path'];
$title = $result['title'];
$detail = $result['detail'];
$priority = $result['priority'];
$active = $result['isActive'];
?><div id="block-image-slider" class="<?php echo(($i%2==0)?'even':'odd')?>">
<h2><?php echo $title ?></h2><span class="operation">[<a href="?action=edit§ion=slider&id=<?php echo $id ?>">កែប្រែ</a>|<a href="?action=delete§ion=slider&id='<?php echo $id ?>'">លុប</a>]</span>
<div class="block-slider-image-body">
<div class="left">
<ul>
<li>លេខរៀងទី<span class="space"><?php echo $id ?></span></li>
<li>កំនត់អទិភាពទី<span class="space"><?php echo $priority ?></span></li>
<li>ត្រូវបានបង្ហាញអោយឃើញ<span class="space"><?php echo (($active==1)?'បង្ហាញ':'មិនបង្ហាញ')?></span></li>
<li>អត្ថបទពេញ<div class="detail"><?php echo $detail ?></div></li>
</ul>
</div>
<div class="right">
<img src="<?php echo '../../image/Slider/'.$img ?>" alt="<?php echo $title ?>" width="170" height="100" />
</div>
<div style="clear:both;"></div>
</div>
</div>
<?php
$j++;
$count++;
$i++;
}
include("../include/paginate.php");
echo paginate_three($reload, $page, $tpages, $adjacents);
这是paginate.php
代码
<?php
function paginate_three($reload, $page, $tpages, $adjacents) {
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "<div class=\"pagin\">\n";
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "?action=slider&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// first
if($page>($adjacents+1)) {
$out.= "<a href=\"" . $reload . "\">1</a>\n";
}
// interval
if($page>($adjacents+2)) {
$out.= "...\n";
}
// pages
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= "<span class=\"current\">" . $i . "</span>\n";
}
elseif($i==1) {
$out.= "<a href=\"" . $reload . "?action=slider\">" . $i . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "?action=slider&page=" . $i . "\">" . $i . "</a>\n";
}
}
// interval
if($page<($tpages-$adjacents-1)) {
$out.= "...\n";
}
// last
if($page<($tpages-$adjacents)) {
$out.= "<a href=\"" . $reload . "?action=slider&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "?action=slider&page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
PS:点击分页页面按钮的最后一个链接时,会显示如上所示的错误消息。而且,我不知道为什么我的数据库中的第一条记录总是显示在每个页面中。任何帮助,将不胜感激。谢谢。
答案 0 :(得分:2)
没有问题。当只有6时,你正在请求第14行。
另外,我认为使用LIMIT [from], [amount]
进行分页会更有效率。
答案 1 :(得分:2)
如果您使用mysql_data_seek()进行分页,那么您已经遇到了大麻烦。
在查询中使用LIMIT运算符