开发者

mysql_fetch_array() expects parameter 1 to be resource, string given

I have two classes DBConn and DBQueries. DBQueries inherits after DBConn. When I'm going to display all users in users database, I see message:

mysql_fetch_array() expects parameter 1 to be resource, string given.
Thanks for any suggestions.

class DBConn /*extends Config*/ {
  public function dbConnection(){
    $db_host = 'localhost';
    $db_login = 'root';
    $db_password = '';
    $db_name = "database";
    $conn = mysql_connect($db_host, $db_login, $db_password);
    $db = mysql_select_db($db_name);
  }  
}
class DBQueries extends DBConn {
  f开发者_运维技巧unction displayUsers(){
    $this->dbConnection();
    $query = "SELECT * FROM users";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($query)) {
      echo $row['password'];
    }
  }
}


You are passing in $query which is your sql string, when you need to pass in $result which is your db result object.

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


You need to pass in the result handle $result returned by mysql_query().


You need to pass $result into the mysql_fetch_array function call as the first parameter, not $query.

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'];
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜