...

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



diload
02-01-2010, 01: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 :

<?php
require_once("constants.php");
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:

<php
require_once('database.class.php');
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 />';
endwhile;
}
?>


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 :)

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



Access denied for user 'ODBC'@'localhost'


The username or password appear to be incorrect.

JAY6390
02-01-2010, 01: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()

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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum