0

I'm using core php, Actually i'm getting post value from submitting my form,i'm going to pass that variable to like query at the time showing issue like :

Fatal error: Cannot use object of type mysqli_result as array in C:\xampp.

I don't know what is the mistake I did please help me.

Here's my code:

if(isset($_POST['list'])){ 
     //-----from post value----//
      $location_type =$_POST['list'];
      $arr = explode(' ',trim($location_type));
      $listing=$arr[0];
      $all_location ="select * from tbl_master_property where pg_address like '%$listing%'";
      $location_result=$conn->query($all_location);
      while($row=mysqli_fetch_assoc($location_result)){ 
      $location_result[] = $row['pg_address'];
      }
      echo "<pre>";print_r($location_result);die;
  } 
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Raj
  • 47
  • 2
  • 9

1 Answers1

0

Problem is you are trying to over-write your mysqli_result variable$location_result inside while() loop.

You need to take a separate array variable to get all the values.

Do like below (and please read comments inside code carefully ):-

 if(isset($_POST['list'])){ 
      $location_type =$_POST['list'];

      $arr = explode(' ',trim($location_type));

      $listing=$arr[0];

      $all_location ="select * from tbl_master_property where pg_address like '%$listing%'";

      $all_location_result = [];//create an array variable
      $location_result=$conn->query($all_location);
      while($row=$location_result->fetch_assoc()){ // try not to mix oop way with procedural way, not a good practic
        $all_location_result[] = $row['pg_address']; // assign values to array variable
      }
      echo "<pre>";print_r($all_location_result);// print array variable
  }

Note:- Youe query code is wide-open for SQL INJECTION so try to use prepared statements

Reference:-

mysqli::prepare

PDO::prepare

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98