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