dou29106 2014-06-13 17:41
浏览 180

如何解决这个错误:mysql_fetch_array()期望参数1是mysqli_result

I am getting this error:

mysql_fetch_array() expects parameter 1 to be mysqli_result

I am trying to do searching a table. I am not sure how my fetch array works with specifically, but I know it returns the row which suits the condition given.

My PHP code :

class Registration
{
    private $db_connection = null;

    public function __construct()
    {
        if (isset($_POST["search"])) 
        {
           $this->doSearch();
        }
    }

   private function doSearch()
   {
     $field = $_POST["field"];
     $find = $_POST["find"];
     echo "you are searching ". $_POST['find'] . " in " . $_POST["field"] . " category"; 
     $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
     $sql = "SELECT * FROM users WHERE upper $field LIKE $find";
     $result_search = $this->db_connection->query($sql); // mysql_fetch_array() expects parameter 1 to be mysqli_result
     while($row = mysqli_fetch_array($result_search)) 
          {
            echo $row['user_name']."</br>";
            echo $row['user_email']."</br>";
            echo $row['user_phone']."</br>";
            echo "<br>";
          }
   }
}

How to solve this problem using OOP.

I can solve in procedural methods but not in OOP.

  • 写回答

1条回答 默认 最新

  • dongwo5110 2014-06-13 18:00
    关注

    I reopened this question because there are at least four issues to correct here.

    First issue: you have syntax errors in your SQL:

     $sql = "SELECT * FROM users WHERE upper $field LIKE $find";
    

    If you wanted to use the upper() function, you need to use parentheses:

     $sql = "SELECT * FROM users WHERE upper($field) LIKE $find";
    

    Second issue: strings must be quoted.

     $sql = "SELECT * FROM users WHERE upper($field) LIKE '$find'";
    

    Third issue: you're copying a variable into your query in an unsafe manner, so you are creating an SQL injection vulnerability. You should use query parameters to avoid this.

     $sql = "SELECT * FROM users WHERE upper($field) LIKE ?";
    

    Fourth issue: it's still unsafe because $field is untrusted input, and could still introduce SQL injection. But query parameters don't work for column names, they only work in place of literal values like a quoted string or a number. The best fix for variable column names is to validate the input against a whitelist of columns that really exist in your table, and reject anything else (or choose a default). Here's a quick way to do that:

     $columns = array("user"=>"user_name", "first"=>"first_name", "last"=>"last_name",
         "DEFAULT"=>"username");
     $realfield = $columns[$field] ?: $columns["DEFAULT"];
     $sql = "SELECT * FROM users WHERE upper($realfield) LIKE ?";
    

    Notice my array keys in $columns don't even have to be exactly the SQL column names. So this allows us to make the web usage friendlier than the SQL usage, and allows us to change one without having to change the other (decoupling).

    Fifth issue: LIKE is already a case-insensitive comparison when using the default collation order, so you don't need upper() anyway. That won't cause an error, but it's just good practice.

     $sql = "SELECT * FROM users WHERE $realfield LIKE ?";
    

    Sixth issue: this is the one that actually caused the error message in your summary. The query() method returns false instead of a statement resource if there's an error. You must always check for that false before trying to use it as though it is a statement.

    Here's a better way to code this, putting it all together:

     $columns = array("user"=>"user_name", "first"=>"first_name", "last"=>"last_name",
         "DEFAULT"=>"username");
     $realfield = $columns[$field] ?: $columns["DEFAULT"];
    
     $sql = "SELECT * FROM users WHERE $realfield LIKE ?";
    
     if (!($stmt = $this->db_connection->prepare($sql))) {
         die($this->db_connection->error);
     }
    
     $stmt->bind_param("s", $find);
     if (!$stmt->execute()) {
         die($stmt->error);
     }
    
     $results = $stmt->get_results();
    
     while($row = $results->fetch_array()) {
         . . .
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向