无法浏览mysql查询结果

时间:2013-03-26 03:05:23

标签: php mysql

有人可以帮我解决返回记录导航的问题吗?我有一个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>

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

修改

我已将代码修改为此(感谢Sean):

$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”返回从零开始的集合。这现在正在运作。 感谢所有贡献者。 干杯,Spud

3 个答案:

答案 0 :(得分:2)

注意:在提醒LIMIT $record_current,1计算机逻辑时 每个计数从零开始,所以如果你想要获得第一个数据,你必须 将您的初始值$record_current设置为0

答案 1 :(得分:1)

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

if($record_current < $view_count)

修改
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows处查看SQL_CALC_FOUND_ROWS / 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);

答案 2 :(得分: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 ...