0
我有一種連接,在php 5.2中正常工作,但現在不更新到5.3版本,它會生成錯誤:「數據庫未選中」。這是我的腳本:數據庫未選擇php 5.3
的config.php:
$host = 'localhost'
$user = 'root'
$password = ''
$db = 'mydb'
Conf.class.php:
class Conf{
private $_userdb;
private $_passdb;
private $_hostdb;
private $_db;
static $_instance;
private function __construct(){
require 'config.php';
$this->_userdb=$user;
$this->_passdb=$password;
$this->_hostdb=$host;
$this->_db=$db;
}
private function __clone(){ }
public static function getInstance(){
if (!(self::$_instance instanceof self)){
self::$_instance=new self();
}
return self::$_instance;
}
public function getUserDB(){
$var=$this->_userdb;
return $var;
}
public function getHostDB(){
$var=$this->_hostdb;
return $var;
}
public function getPassDB(){
$var=$this->_passdb;
return $var;
}
public function getDB(){
$var=$this->_db;
return $var;
}
}
Db.class.php:
class Db {
private $server;
private $user;
private $password;
private $data_base;
private $link;
private $result;
static $_instance;
private function __construct() {
$this->setConnection();
$this->connect();
$this->result = null;
}
private function setConnection() {
$conf = Conf::getInstance();
$this->server = $conf->getHostDB();
$this->data_base = $conf->getDB();
$this->user = $conf->getUserDB();
$this->password = $conf->getPassDB();
}
private function __clone(){ }
public static function getInstance() {
if (!(self::$_instance instanceof self)){
self::$_instance=new self();
} return self::$_instance;
}
private function connect() {
$link=mysql_connect($this->server, $this->user, $this->password);
if ($link){
mysql_select_db($this->data_base,$link);
}
if (!$link){
die('Can not connect');
}else{
$this->link = $link;
}
}
}
顯然Db.class類,不操作函數getInstance(),因爲沒有從Conf.class獲取數據。
我需要改變在Db.class和Conf.class中的東西?