我有一个我无法弄清楚的问题。我有一个类数据库,如果我直接使用它,我会定期连接到数据库。我有另一个类Categories,我想调用一个Database对象。问题是,如果我调用$ db->连接类别不起作用。我尝试直接在类别中调用mysql_connect,它工作正常!
为什么我不能使用$db->connect (the error is Access denied for user 'user'@'0.0.0.0' (using password: YES)
。
我在类Database中的代码是:
public function connect($new_link=false){
$this->link_id = @mysql_connect($this->server,$this->user,$this->pass,$new_link);
echo "<br/>link_id = ".$this->link_id;
if (!$this->link_id){//open failed
$this->oops("Could not connect to server: <b>$this->server</b>.");
}
else{
echo "Connected to server <br/>";
}
if(!@mysql_select_db($this->database, $this->link_id)){//no database
$this->oops("Could not open database: <b>$this->database</b>.");
}
else{
echo "Database opened <br/>";
}
// unset the data so it can't be dumped
$this->server='';
$this->user='';
$this->pass='';
$this->database='';
}#-#connect()
我在Class类中的代码是:
public static function selectAll() { // SELECT All Function
$db = Database::obtain();
// connect to the server
$db->connect();
$sql = "SELECT * FROM productCategory";
$rows = $db->fetch_array($sql);
return $rows;
}
数据库::获取代码
public static function obtain($server=null, $user=null, $pass=null, $database=null){
if (!self::$instance){
self::$instance = new Database($server, $user, $pass, $database);
}
return self::$instance;
}#-#obtain()
我做错了,我看不到?
答案 0 :(得分:0)
好吧,错误信息是“访问被拒绝用户'user'@'0.0.0.0'” 这意味着当您使用$ db = Database :: gets();实例化您的类时您没有为server,user,pass,database设置值。类Database可能实现了单例模式,而方法gets()只返回没有设置任何属性的实例。