【问题标题】:Database class is not correctly connecting to my database数据库类未正确连接到我的数据库
【发布时间】:2010-05-01 18:43:04
【问题描述】:

我只是冒险进入 OOP 的世界,所以如果这是一个卑鄙的问题,请原谅我。

这是我在index.php 上的内容:

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

这是数据库类:

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());
    }
}

我已将其缩短为 connect() 方法以简化操作。

这是 RS 类:

class RS
{
    private $username;
    private $password;

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

    }
}

如您所见,我需要在我的 RS 类中访问和使用数据库类。但是当我加载页面时出现此错误:

警告:mysql_connect() [function.mysql-connect]:访问 拒绝用户 'ODBC'@'localhost' (使用密码:NO)在 C:\xampp\htdocs\includes\database.class.php 在第 22 行

问题是我不知道它从哪里得到它需要使用ODBC 作为用户的想法......我已经阅读了关于做这些东西的内容,并且从我可以收集到的信息中我做得正确。

谁能帮帮我?

谢谢。

【问题讨论】:

  • blerh,请投票并接受答案。

标签: php database class object


【解决方案1】:

connect() 中的属性名与类的属性名不匹配。

变化:

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

收件人:

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

然后改变:

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

收件人:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多