1

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.

6
  • 1
    use mysql's RAND() Commented Apr 20, 2016 at 6:52
  • i used this.its not working as excepeted. Commented Apr 20, 2016 at 6:52
  • 1
    on a sidenote: please make your code more readable - I can't even think about working with code formatted like yours! Commented Apr 20, 2016 at 6:52
  • 1
    I agree with @low_rents, please make the code more readable. You'll get more people answering you that way. Good for you, good for our eyes, good for our long lives... now that was a master rhyme. Commented Apr 20, 2016 at 6:55
  • @Webeng yeah - seems like many PHP beginners are afraid of using line breaks in their code. you can use a line break almost anywhere in your PHP code as long as you don't use them inside words (e.g. variables) or operators. you have got the semicolon (;) to mark the end of a command in PHP! Commented Apr 20, 2016 at 7:01

3 Answers 3

3

Use

ORDER BY rand()

This will generate random results.

MySQL Reference

Sign up to request clarification or add additional context in comments.

3 Comments

i used this but no changes at each time of call.
@ Pupil : i used like this $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 ORDER BY rand()";.but its not changing any order in each time of call that url.
Please check how many records you have in database. Also, print the query and execute in MySQL client like PHPMyAdmin. The syntax of ORDER BY RAND() is perfect and it must work.
1

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

Comments

1

Yes

ORDER BY rand()

It will generate random results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.