private static $_instance = null;
function __construct($host,$user,$password,$database)
{
$this->host = $host;
$this->user = $user;
$this->passwd = $password;
$this->database = $database;
if (self::$_instance == null)
{
self::$_instance=$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or die("Could not connect to $this->host");
}else{
$this->conn=self::$_instance;
}
mysql_select_db($this->database,$this->conn) or die("Could not switch to database $this->database");
mysql_query("set names 'utf8'");
}
代码如上所示,目前需要把之前的数据库操作类改成单例模式,但是我只打算改数据库,但是现在还不确定多次实例化 是用的同一个连接 还是每次实例化都会重新连接一次
function __construct($host,$user,$password,$database)
{
$this->host = $host;
$this->user = $user;
$this->passwd = $password;
$this->database = $database;
if (self::$_instance == null)
{
self::$_instance=$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or die("Could not connect to $this->host");
}else{
$this->conn=self::$_instance;
}
mysql_select_db($this->database,$this->conn) or die("Could not switch to database $this->database");
mysql_query("set names 'utf8'");
}
代码如上所示,目前需要把之前的数据库操作类改成单例模式,但是我只打算改数据库,但是现在还不确定多次实例化 是用的同一个连接 还是每次实例化都会重新连接一次