简体   繁体   中英

How to fetch value from Database in Random order using MySQL and PHP

I need one help.i need to fetch value from table in Random order at each time of call using MySQL and PHP.I am explaining my code below.

$subcat_id=$_GET['subcat_id'];
$quad_id=$_GET['quad_id'];
$day_id=$_GET['day_id'];
$data=array();
if(!empty($quad_id) && !empty($day_id)){
$sqlqry=mysqli_query($connect,"select * from db_restaurant_detail where subcat_id='".$subcat_id."' and day_id='".$day_id."'");
if(mysqli_num_rows($sqlqry) > 0){
    while($row=mysqli_fetch_array($sqlqry)){
        $member_id=$row['member_id'];
        $qry="SELECT c.member_id,c.rest_name,c.quadrant,c.city,c.proviance,c.postal,c.address,c.country,c.person,c.mobile,c.url,c.  premium,c.image,c.multiple_image,c.business_phone_no,q.quadrant AS quadrant_name FROM db_restaurant_basic AS c LEFT JOIN db_quadrant AS q ON c.quadrant=q.quad_id WHERE c.member_id='".$member_id."' and c.quadrant='".$quad_id."' and c.status=1";
        $fetchqry=mysqli_query($connect,$qry);
        if(mysqli_num_rows($fetchqry) > 0){
         while($row1=mysqli_fetch_array($fetchqry)){
                      if($row1['multiple_image']==''){
                          $available_image=false;
                      }else{
                           $available_image=true;
                      }
                      $data[]=array("day_id"=>$row['day_id'],"comment"=>$row['comment'],"restaurant_name"=>$row1['rest_name'],"member_id"=>$row1['member_id'],"available_image"=>$available_image,"quadrant"=>$row1['quadrant_name'],"address"=>$row1['address'],"city"=>$row1['city'],"proviance"=>$row1['proviance'],"postal_code"=>$row1['postal'],"country"=>$row1['country'],"person"=>$row1['person'],"mobile"=>$row1['mobile'],"url"=>$row1['url'],"premium"=>$row1['premium'],"image"=>$row1['image'],"business_phone_no"=>$row1['business_phone_no']);
                  }
                  $result=array("data"=>$data,"imagepath"=>$imagepath);
                  }else{
                     $result=array("data"=>$data,"imagepath"=>$imagepath); 
                  }
              }
            }else{
                $result=array("data"=>$data,"imagepath"=>$imagepath);
            }

When user is invoking this localhost/spesh/mobileapi/categoryproduct.php?item=1&acn=2&subcat_id=4&quad_id=5 the above code is executing.Here I need when user is calling that url each time the required data should come randomly not in a asc or desc order.Please help me.

Use

ORDER BY rand()

This will generate random results.

MySQL Reference

You can add "ORDER BY rand() " in your query. So it will fetch random data.

Yes

ORDER BY rand()

It will generate random results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM