Hello,
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:
index.php
[php]
<?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]
database.interface.php:
[php]
<?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]
mysql_database.class.php:
[php]
<?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); } } ?>[/php]
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!