douzhizao0270 2016-01-20 07:25
浏览 27
已采纳

从数据库中检索多个记录

In my database 'coordinates' there are four columns id bus_id latitude longitude where a user having a particular bus_is send its long, lat on a regular interval of time. I want to retrieve most updated value for each user. Here is my code:

<?php
      define('HOST','localhost');
      define('USER','root');
      define('PASS','1234');
      define('DB','coordinates');
      $con = mysqli_connect(HOST,USER,PASS,DB);


     $arr=[];
      for($x=1; $x=3; $x++){
      $query = "SELECT id, longitude, latitude FROM data WHERE bus_id= ".$x." ORDER BY id DESC limit 1 offset 1 " ;
      $qry_result = mysqli_query($con,$query);// or die(mysqli_error());
      }   
       // Insert a new row in the table for each person returned
       while($row = mysqli_fetch_array($qry_result)) {

         array_push($arr, [
           'lat'=>['$latitude'],
           'lng'=>['$longitude'],
           //'recs'=>$recs
         ]);
       }

$record= json_encode($arr);
echo $arr;    
?>

when i run this code it doesn't give me right output. Please tell me where am i wrong.

  • 写回答

3条回答 默认 最新

  • dongpiansui8755 2016-01-20 07:29
    关注

    You should run the while into the for loop. Try this code :

        <?php
          define('HOST','localhost');
          define('USER','root');
          define('PASS','123456');
          define('DB','test');
          $con = mysqli_connect(HOST,USER,PASS,DB);
    
         $arr=[];
          for($x=1; $x<=3; $x++){
           $query = "SELECT id, longitude, latitude FROM data WHERE bus_id= ".$x." ORDER BY id DESC limit 1" ;
    
          $qry_result = mysqli_query($con,$query);// or die(mysqli_error());
        // Insert a new row in the table for each person returned
           while($row = mysqli_fetch_array($qry_result)) {
    
             array_push($arr, [
               'lat'=>[$row['latitude']],
               'lng'=>[$row['longitude']],
               //'recs'=>$recs
             ]);
           }
          }   
    
    
    $record= json_encode($arr);
    echo $record;    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题