Mysql & PHP error (OOP)


I’ve just started learning OOP and I made a piece of code but I just can’t figure out why it isn’t working.
Does anyone have any idea?

This is error that I’m getting:

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\OOP\classes\mysql_database.class.php on line 34

This is my code:



<?php include 'interfaces/database.interface.php'; include 'classes/mysql_database.class.php'; $db = new Mysql_Database(); $db->connect('localhost', 'root', ''); $db->query('test'); $result = $db->query("SELECT username FROM users"); while($row = $db->fetchAssoc($result)) { echo($row['username']); } ?>




<?php interface Database{ public function connect(); public function error(); public function errno(); public function escape($string); public function query($query); public function fetchArray($result); public function fetchRow($result); public function fetchAssoc($result); public function fetchObject($result); public function numRows($result); public function close(); } ?>




<?php class Mysql_Database implements Database { private $_link; public function connect($server='', $username='', $password='', $new_link=true, $client_flags=0) { $this->_link = mysql_connect($server, $username, $password, $new_link, $client_flags); } public function error() { return mysql_errno($this->_link); } public function errno() { return mysql_error($this->_link); } public function escape($string) { return mysql_real_escape_string($string, $this->_link); } public function query($query) { return mysql_query($query, $this->_link); } public function fetchArray($result, $array_type = MYSQL_BOTH) { return mysql_fetch_array($result, $array_type); } public function fetchRow($result) { return mysql_fetch_row($result); } public function fetchAssoc($result) { return mysql_fetch_assoc($result); } public function fetchObject($result) { return mysql_fetch_object($result); } public function numRows($result) { return mysql_num_rows($result); } public function close() { return mysql_close($this->_link); } } ?>


On my wamp server I have a database called ‘test’, with a table named users and in the table I have a row id and a row username.

Thanks in advance!

The error kind of explains itself, but I’ll talk you through it. mysql_fetch_assoc expects the first parameter to be a resource (a MySQL Result Resource from a function such as mysql_query). Instead a boolean has been given, which means that the function that was supposed to generate a result resource (mysql_query) has instead returned a boolean value.

If you read the documentation on (mysql_query) you will see that mysql_query returns boolean FALSE when an error has occurred (due to insufficient permissions, or, more likely, invalid syntax in the query) - this is also a good time to draw attention to the message at the top of that page, you should no longer use mysql_* functions, as it is being deprecated, you should move on to MySQLi or, preferably, PDO. Anyway, we’ll stick with mysql_* for now, but be aware of it’s future demise.

Long story short - your query failed and returned false. Check out mysql_error() after the query has been run to see what error MySQL threw out.

Sponsor our Newsletter | Privacy Policy | Terms of Service