开发者

Database class is not correctly connecting to my database

开发者 https://www.devze.com 2022-12-29 08:13 出处:网络
I\'m just venturing into the world of OOP so forgive me if this is a n00bish question. This is what I have on index.php:

I'm just venturing into the world of OOP so forgive me if this is a n00bish question.

This is what I have on index.php:

$dbObj = new Database();
$rsObj = new RS($dbObj);

This is the Database class:

class Database
{
    private $dbHost;
    private $dbUser;
    private $dbPasswd;
    private $dbName;
    private $sqlCount;

    function __construct()
    {
        $this->dbHost   = 'localhost';
        $this->dbUser   = 'root';
        $this->dbPasswd = '';
        $this->dbName   = 'whatever';
        $this->sqlCount = 0;
    }

    function connect()
    {
        $this->link = mysql_connect($this->db_host, $this->db_user, $this->db_passwd);
        if(!$this->link)
                $this->error(mysql_error());

        $this->selection = mysql_select_db($this->db_name, $this->link);
        if(!$this->selection)
                $this->error(mysql_error());
    }
}

I've shortened it to just the connect() method to simplify things.

This is the RS class:

class RS
{
    private $username;
    private $password;

    function __construct($dbObj)
    {
        // We need to get an account from the db
        $dbObj->connect();

    }
}

As you can probably see, I need to access and use the database class in my RS class. But I get this error when I load the page:

开发者_如何学编程

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\includes\database.class.php on line 22

The thing is I have NO idea where it got the idea that it needs to use ODBC as a user... I've read up on doing this stuff and from what I can gather I am doing it correctly.

Could anyone lend me a hand?

Thank you.


The property names in connect() do not match the property names of the class.

Change:

$this->link = mysql_connect($this->db_host, $this->db_user, $this->db_passwd);

To:

$this->link = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd);

And change:

$this->selection = mysql_select_db($this->db_name, $this->link);

To:

$this->selection = mysql_select_db($this->dbName, $this->link);
0

精彩评论

暂无评论...
验证码 换一张
取 消