【问题标题】:Fatal error: Cannot use object of type mysqli_result as array in C:\xampp\致命错误:不能使用 mysqli_result 类型的对象作为 C:\xampp\ 中的数组
【发布时间】:2018-08-25 02:01:58
【问题描述】:

我正在使用核心 php,实际上我是通过提交表单来获取 post 值,我将在显示问题时将该变量传递给 like 查询:

致命错误:不能使用 mysqli_result 类型的对象作为数组 C:\xampp.

我不知道我做错了什么请帮助我。

这是我的代码:

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;
  } 

【问题讨论】:

  • Raj 错误消息也会明确告诉您行号。请说明是哪一行导致了问题

标签: php arrays mysqli


【解决方案1】:

问题是您试图在while() 循环中覆盖您的mysqli_result 变量$location_result

你需要一个单独的数组变量来获取所有的值。

如下所示(请仔细阅读cmets里面的代码):-

 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
  }

注意:- Youe 查询代码对SQL INJECTION 开放,因此请尝试使用prepared statements

参考:-

mysqli::prepare

PDO::prepare

【讨论】:

  • @Raj 很高兴为您提供帮助:):)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多