无法使用PHP和MySQL随机获取数据

时间:2016-05-31 10:08:37

标签: php mysql

我需要一个帮助。我正在尝试使用PHP和MySQL随机获取数据,但它不会发生这种情况。我在下面解释我的代码。

$day_id=$_GET['day_id'];
$sql=mysqli_query($connect,"select * from db_restaurant_basic where premium=1 and status=1 order by member_id,rand()");
if(mysqli_num_rows($sql) > 0){
    while($row=mysqli_fetch_array($sql)){
          $member_id=$row['member_id'];
          $quad_id=$row['quadrant'];
           $sqlqry=mysqli_query($connect,"select * from  db_restaurant_detail where member_id='".$member_id."' and day_id='".$day_id."' and checked=1" );
          while($details=mysqli_fetch_array($sqlqry)){
                        $data[]=array("day_id"=>$details['day_id'],"comment"=>$details['comment'],"restaurant_name"=>$row['rest_name'],"member_id"=>$row['member_id'],"available_image"=>$available_image,"city"=>$row['city'],"proviance"=>$row['proviance'],"postal_code"=>$row['postal'],"country"=>$row['country'],"person"=>$row['person'],"mobile"=>$row['mobile'],"url"=>$row['url'],"premium"=>$row['premium'],"image"=>$row['image'],"business_phone_no"=>$row['business_phone_no']);
          }
    }
}
$result=array("data"=>$data,"imagepath"=>$imagepath);
echo json_encode($result,JSON_UNESCAPED_SLASHES);

从上面的查询我无法获取随机数据。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

仅使用ORDER BY RAND()获取随机数据:

$sql=mysqli_query($connect,"select * from db_restaurant_basic where premium=1 and status=1 ORDER BY RAND()");