数据库类没有正确连接到我的数据库
问题描述:
我只是冒险进入OOP的世界,所以请原谅我,如果这是一个n00bish问题。数据库类没有正确连接到我的数据库
这是我对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-连接]:拒绝用户 'ODBC' 访问 @ 'localhost' 的 (使用密码:NO)在 C:\ XAMPP \ htdocs中\包括\在线22
事情database.class.php 是我不知道它在哪儿了,它需要使用ODBC
作为用户的想法...我已经读过关于做这些事情的信息,并且从我能收集到的信息中我正确地做到了。
任何人都可以借我一只手吗?
谢谢。
答
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);
blerh,请v ote并接受答案。 – webbiedave 2010-05-01 19:00:58