Monday, September 23, 2019

php - 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 {
function displayUsers(){
$this->dbConnection();
$query = "SELECT * FROM users";
$result = mysql_query($query);
while ($row = mysql_fetch_array($query)) {
echo $row['password'];
}
}
}


Answer



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

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...