douxiangdiao6348 2013-03-26 03:05
浏览 44
已采纳

无法浏览mysql查询结果

Can someone please assist me with a problem of navigating through returned records? I have a mysql query:

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);

The query produces the results desired. The values are used to fill a dynamic form. The problem I am having is navigating through the records properly. I am using this code for navigation (to allow a user to see all the bookings they have made). The code:

<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>

The problem is that the first record to be displayed is actually the 2nd record from the query and the 'Next' link displays even when the last record is displayed. If the link is clicked it shows a blank form. I don't know what I am doing wrong and I've played with this for hours, any help would be greatly appreciated. Cheers

Edit

I have amended the code to this (thanks to 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);

Apparently, "SELECT FOUND_ROWS" returns a set starting from zero. This is now working. Thanks to all who contributed. Cheers, Spud

  • 写回答

3条回答 默认 最新

  • douna5529 2013-03-26 03:42
    关注

    Your if() in your NEXT line is wrong, as you are checking $record_current against the query - $view_result instead of the number of rows - $view_count. Try changing to -

    if($record_current < $view_count)
    

    EDIT
    Take a look at SQL_CALC_FOUND_ROWS / FOUND_ROWS() at 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?