0

有人可以帮我解决浏览返回记录的问题吗?我有一个mysql查询:

if(isset($_GET['record'])) {
$record_current = $_GET['record'];
}else{
$record_current = 1;
}

$useremail = $_SESSION['useremail'];

$view_sql = "SELECT * FROM missions, calendar, bookings 
WHERE missions.missions_id =  calendar.missions_id 
AND calendar.calendar_id = bookings.calendar_id 
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";

$view_result = mysql_query($view_sql) or die(mysql_error());
$view_count = mysql_num_rows($view_result);
$rsView = mysql_fetch_assoc($view_result);

查询产生所需的结果。这些值用于填写动态表单。我遇到的问题是正确浏览记录。我正在使用此代码进行导航(以允许用户查看他们所做的所有预订)。编码:

<span class="label"><?php if($record_current > 1) { ?><a href="view.php?record=<?php echo $record_current - 1; ?>" >Previous</a><?php } ?></span>
<span class="element"><?php if($record_current < $view_result) { ?><a href="view.php?record=<?php echo $record_current + 1; ?>" >Next</a><?php } ?></span>

问题是要显示的第一条记录实际上是查询中的第二条记录,即使显示最后一条记录,也会显示“下一个”链接。如果单击该链接,则会显示一个空白表单。我不知道我做错了什么,我已经玩了几个小时,任何帮助将不胜感激。干杯

编辑

我已经修改了代码(感谢肖恩):

$view_sql = "SELECT SQL_CALC_FOUND_ROWS * FROM missions, calendar, bookings 
WHERE missions.missions_id =  calendar.missions_id 
AND calendar.calendar_id = bookings.calendar_id 
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";

$view_result = mysql_query($view_sql) or die(mysql_error());
$view_count = mysql_query("SELECT FOUND_ROWS() AS cnt");
$rsView = mysql_fetch_assoc($view_result);
$adj_count = (mysql_result($view_count, 0, "cnt") - 1);

显然,“SELECT FOUND_ROWS”返回一个从零开始的集合。现在正在工作。感谢所有做出贡献的人。干杯,土豆

4

3 回答 3

2

注意:在LIMIT $record_current,1提醒计算机逻辑时,每个计数都从零开始,因此如果您尝试获取第一个数据,则必须将初始值设置$record_current为 0

于 2013-03-26T04:01:50.473 回答
1

if()NEXT行是错误的,因为您正在检查$record_current查询 -$view_result而不是行数 - $view_count。尝试更改为 -

if($record_current < $view_count)

编辑
看看SQL_CALC_FOUND_ROWS/FOUND_ROWS()http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

$view_sql = "SELECT SQL_CALC_FOUND_ROWS * FROM missions, calendar, bookings 
WHERE missions.missions_id =  calendar.missions_id 
AND calendar.calendar_id = bookings.calendar_id 
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";

$view_result = mysql_query($view_sql) or die(mysql_error());
$view_count = mysql_query("SELECT FOUND_ROWS()");
$rsView = mysql_fetch_assoc($view_result);
于 2013-03-26T03:42:20.270 回答
1
if(isset($_GET['record'])) {
$record_current = $_GET['record'];
}else{
$record_current = 1;
}

$view_sql = "SELECT * FROM missions, calendar, bookings 
WHERE missions.missions_id =  calendar.missions_id 
AND calendar.calendar_id = bookings.calendar_id 
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";

问题是要显示的第一条记录实际上是查询中的第二条记录

请注意,当您使用 LIMIT 时,第一行的偏移量将始终为 0 而不是 1...

于 2013-03-26T04:22:34.807 回答