2013-03-14 51 views
0

我想知道是否有一種方式,我總是有一個打開連接到我的數據庫。 我用了我的網站這個代碼:只有一個MySQL連接在所有項目

的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個連接?

+0

它的繼承.... – 2013-03-14 05:45:15

+0

是的,我的對象都從模型類和模型從mysql繼承 – 2013-03-14 05:48:38

+0

是的,因爲它調用它的構造函數方法並創建mysql連接。 – 2013-03-14 05:50:15

回答

0

您的設計並沒有多大的意義。您應該爲MySQLModel創建單獨的類。 MySQL類應該實例化一次,並在整個腳本中使用。所以它應該只有一個連接。 然後讓Model使用MySQL類的實例對數據進行操作。

相關問題