View Full Version : Resolved OO PHP and mysql help please :)

02-01-2010, 02:11 AM
Okay I've just started on some of my first OO PHP but I'm getting an mysql error all the time.

The error is :
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO)

and it comes when this function is called:
public function query($query){
return mysql_query($query);

and the while document where this function lives in is :

class DILOAD_db

public $connection; //The MySQL database connection

/* Class construcror */
private function MySQLDB(){
/* Make connection to database */
$this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
/* Function for a query */
public function query($query){
return mysql_query($query) or die(mysql_error());

/* Creates database connection and the object*/
$database = new DILOAD_db;

And the file where I'm calling this in looks like this:

class DILOAD_news
public function list_pages(){
$query = DILOAD_db::query("SELECT * FROM pages"); // <-- IT IS CALLED HERE!
while($page = mysql_fetch_assoc($query)):
echo '<a href="./page.php?id='. $page['id'] . '">' . $page['name'] . '</a><br />';

I have also tried to make the query without the function and just a regular mysql_query but it just gives the same error. The error is only when I'm doing that query and I'm 100% sure that the constants used for connection is correct and I have also tried to connect without constants.

and I know its not the best code ever but its my start to OO PHP but I'd be happy to take any tips hehe :)

btw ANY help is appreciated !

Regards :)

02-01-2010, 02:18 AM
The error details are fairly self explanatory.

Access denied for user 'ODBC'@'localhost'

The username or password appear to be incorrect.

02-01-2010, 02:23 AM
Firstly, you're using the class in two ways. You're creating a new class with the $database = new DILOAD_db; and then you're calling it with the static function DILOAD_db::query.

You should pass the db object to your class such as adding a new __construct function to the class, where you pass the $db object, then run $db->query()

02-01-2010, 02:27 AM
never mind found out that my constructor was named wrong ......
sorry >.>

02-01-2010, 02:28 AM
And thanks btw :)