This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                2年前关闭。
            
                    
我有两个类DBConnDBQueriesDBQueriesDBConn之后继承。当我要在用户数据库中显示所有用户时,我看到消息:mysql_fetch_array()期望参数1为资源,给定字符串。感谢您的任何建议。

类DBConn / *扩展Config * / {
  公共函数dbConnection(){
    $ db_host ='本地主机';
    $ db_login ='root';
    $ db_password ='';
    $ db_name =“数据库”;
    $ conn = mysql_connect($ db_host,$ db_login,$ db_password);
    $ db = mysql_select_db($ db_name);
  }
}


DBQueries类扩展了DBConn {
  函数displayUsers(){
    $ this-> dbConnection();
    $ query =“选择*来自用户”;
    $ result = mysql_query($ query);
    while($ row = mysql_fetch_array($ query)){
      echo $ row ['password'];
    }
  }
}

最佳答案

当您需要传递$query作为数据库结果对象时,您传递的是SQL字符串$result

class DBQueries extends DBConn {
    function displayUsers(){
        $this->dbConnection();
        $query = "SELECT * FROM users";
        $result = mysql_query($query);
        while ($row = mysql_fetch_array($result)) {
            echo $row['password'];
        }
    }
}

关于php - mysql_fetch_array()期望参数1为资源,给定字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30600565/

10-14 07:28