我想知道是否有一种方法可以让我始终打开一个连接到我的数据库。我在我的网站中使用此代码:
mysql 类
class Mysql{
public static $Server = "localhost";
static $User = 'root';
static $Password = '';
static $DataBaseName = 'teca.v1';
static $Connection;
static $DataBase;
static $LoadConnection = 0;
static $CharSet = "utf8";
static $TRANS =false;
static $TRSS = array();
public function __construct(){
if(Mysql::$LoadConnection==0){
Mysql::$Connection = mysql_connect(Mysql::$Server,Mysql::$User,Mysql::$Password);
Mysql::$DataBase = mysql_select_db(Mysql::$DataBaseName,Mysql::$Connection);
$charset=Mysql::$CharSet;
mysql_query("SET NAMES $charset");
Mysql::$LoadConnection=1;
}
}
}
模型类
class Model extends Mysql{
protected $datamembers = array();
protected $PrimaryKey="Id";
protected $TableName ="Table";
public $UnProtectedExecute = FALSE;
public function ReadyString($value)
{
if($this->UnProtectedExecute == TRUE)
return addslashes($value);
else
return $value;
}
public function __set($variable, $value){
$this->datamembers[strtolower($variable)] = $value;
}
public function __get($variable){
return $this->datamembers[strtolower($variable)];
}
和我的一个对象:
class LibraryFiles extends Model{
public $TableName = 'library_files';
}
但我不知道这是否正确,只是这段代码为我的项目中的所有对象创建了 1 个连接?