dongliu5475 2013-12-19 03:05
浏览 21
已采纳

mysqli限制产生的结果超过限制

I'm using this to fetch results from database.

$from=$to; ////limit from
$to=$from+5; ////limit to
$search_query="SELECT * FROM `user_info_secondary` WHERE `city`='$query' LIMIT $from,$to"

When i set $to=0; the search yields exactly 5 results but when i set to $to=5,10,15.... its shows me 6 results why is that?

$from=$to;
$to=$from+5;
$search_query="SELECT * FROM `user_info_secondary` WHERE `city`='$query' LIMIT $from,$to";
$do_search=  mysqli_query($connection, $search_query);
$number_of_results= mysqli_num_rows($do_search);
while($number_of_results>0)
{
       $get_result_details= mysqli_fetch_array($do_search);
       $search_result_details= $get_result_details['username'];
       echo $search_result_details;
       --$number_of_results;
} 
  • 写回答

1条回答 默认 最新

  • duanhao4156 2013-12-19 03:13
    关注

    As Barmar said, it's not from, to - its offset, count

    offset of 5 means item # 4 is where it starts

    [0, 1, 2, 3, 4] 
    

    and count of 10 results in:

    0, 1, 2, 3 [START HERE] -> 4, 5, 6, 7, 8, 9 //(because 0 is part of that count so 9 is last). 
    

    aka 6 items

    offset 0 means you start at item # 0 and count of 5 results in

    [START HERE] -> 0, 1, 2, 3, 4
    

    aka 5 items

    Just do this to fix it:

    $from = ##;  // Where ## is the number you choose
    $to = ($from > 0) ? $from + 4 : $from + 5;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向