2009-09-09 31 views
5

我有一個很大的網站,每個頁面都是從幾個包含的文件構建的,我的網站是100%的程序格式,我正在學習在PHP中使用類和更多的OOP方法。我應該在PHP中重新連接到mysql嗎?

目前我的網站有一個包含在每個頁面中的頭文件,在這個頭文件中是一個mysql連接,並且持續了頁面的持續時間,所以如果我需要運行來自不同文件的10個不同查詢,它們全部運行時不需要建立新的連接,所以連接只進行一次。

現在我試圖轉換爲更多的OO方式,我開始編寫一個mysql類來連接和運行查詢,所以我正在考慮使用類__construct函數來連接到mysql,我是隻是好奇它是如何工作的,每當這個類被調用時,它會使或連接到mysql而不是一次。

也許我沒有清楚地想清楚。我是否應該在標題中首次發起這個課程,然後我不必再擔心了?

回答

6

您可以創建MySQL類的單個全局對象並在任何地方使用該對象。那麼你的構造函數只會被調用一次。

或者你可以在任何地方創建MySQL類的新對象。 mysql_connect不打開新的連接,如果已經有一個開放:

如果第二個呼叫對mysql_connect()使用相同的參數,沒有新的鏈接將被建立,而是的鏈路標識符已打開的鏈接將被退回。

+0

你也可以編寫一個單獨的類來連接數據庫,這樣只有一個連接打開,它是一致的,它是乾的。 – 2009-09-09 11:33:43

+1

呃......我的意思是單身類。哦,我討厭這些星期幾乎沒有任何東西,但星期一...... – 2009-09-09 11:34:20

+0

是的,那是我沒有想到的另一個解決方案。 – 2009-09-11 17:21:07

-2

如果使用mysql_pconnect()函數可以使用該方法,如果已經有一個MySQL連接至極將搜索並且如果它發現它,它不會再創建一個。

在可替代的,如果你考慮,也沒有在PHP中使用的情況下,可以直接調用PHP數據庫對象,如:

class DB {} 

DB::connect($host, $user, $pass); 

如果使用這種方法,你不必擔心多個連接。當然,如果你需要同時使用多個數據庫連接,你可以使用對象實例,或者創建你的類,以便它可以接受多個參數並一次存儲它們(不是很贊同這個)

+0

不要誤導有關使用mysql_pconnect()的問題。 – 2009-09-09 07:06:38

+0

mysql_pconnect:首先,連接時,函數首先會嘗試找到已經用相同的主機,用戶名和密碼打開的(持久)鏈接。如果找到一個,那麼它的標識符將被返回而不是打開一個新的連接。 來源:php.net 又是什麼毛病? – yoda 2009-09-09 07:08:43

1

是的,你不應該連接多次。始終打開和關閉連接的開銷大於在腳本運行相對較短的時間內保持打開的開銷。所以你應該在開始時創建一個類的實例並保存在一個全局變量中。

這當然不是一個壞主意,編寫自己的類的運動,但也許你應該考慮數據庫連接管理中存在的解決方案之一(Zend_Db的等)。

0

您可以隨時將數據庫鏈接引用存儲在STATIC類變量中,並在需要時調用它。但是,PHP本身嘗試使用存在於內存中的現有鏈接。

我有一個示例數據庫處理程序代碼爲你着,當然它的PHP 5,並使用PDO。

<?php 
// Class providing generic data access functionality 
class DatabaseHandler 
{ 
    // Hold an instance of the PDO class 
    private static $_mHandler; 

    // Private constructor to prevent direct creation of object 
    private function __construct() 
    { 
    } 

    // Return an initialized database handler 
    private static function GetHandler() 
    { 
    // Create a database connection only if one doesn’t already exist 
    if (!isset(self::$_mHandler)) 
    { 
     // Execute code catching potential exceptions 
     try 
     { 
     // Create a new PDO class instance 
     self::$_mHandler = 
      new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD); 

     // Configure PDO to throw exceptions 
     self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE, 
             PDO::ERRMODE_EXCEPTION); 
     self::$_mHandler->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); 
     } 
     catch (PDOException $e) 
     { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
     } 
    } 

    // Return the database handler 
    return self::$_mHandler; 
    } 
    // Clear the PDO class instance 
    public static function Close() 
    { 
    self::$_mHandler = null; 
    } 
    // Wrapper method for PDO::prepare 
    private static function Prepare($queryString) 
    { 
    // Execute code catching potential exceptions 
    try 
    { 
     // Get the database handler and prepare the query 
     $database_handler = self::GetHandler(); 
     $statement_handler = $database_handler->prepare($queryString); 

     // Return the prepared statement 
     return $statement_handler; 
    } 
    catch (PDOException $e) 
    { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
    } 
    } 

    // Wrapper method for PDOStatement::execute() 
    public static function Execute($sqlQuery, $params = null) 
    { 
    // Try to execute an SQL query or a stored procedure 
    try 
    { 
     $statement_handler = self::Prepare($sqlQuery); 

     // Execute query 
     $statement_handler->execute($params); 
    } 
    // Trigger an error if an exception was thrown when executing the SQL query 
    catch(PDOException $e) 
    { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
    } 
    } 

    // Wrapper method for PDOStatement::fetchAll() 
    public static function GetAll($sqlQuery, $params = null, 
           $fetchStyle = PDO::FETCH_ASSOC) 
    { 
    // Initialize the return value to null 
    $result = null; 

    // Try to execute an SQL query or a stored procedure 
    try 
    { 
     $statement_handler = self::Prepare($sqlQuery); 

     // Execute the query 
     $statement_handler->execute($params); 

     // Fetch result 
     $result = $statement_handler->fetchAll($fetchStyle); 
    } 
    // Trigger an error if an exception was thrown when executing the SQL query 
    catch(PDOException $e) 
    { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
    } 

    // Return the query results 
    return $result; 
    } 

    // Wrapper method for PDOStatement::fetch() 
    public static function GetRow($sqlQuery, $params = null, 
           $fetchStyle = PDO::FETCH_ASSOC) 
    { 
    // Initialize the return value to null 
    $result = null; 

    // Try to execute an SQL query or a stored procedure 
    try 
    { 

     $statement_handler = self::Prepare($sqlQuery); 

     // Execute the query 
     $statement_handler->execute($params); 

     // Fetch result 
     $result = $statement_handler->fetch($fetchStyle); 
    } 
    // Trigger an error if an exception was thrown when executing the SQL query 
    catch(PDOException $e) 
    { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
    } 

    // Return the query results 
    return $result; 
    } 

    // Return the first column value from a row 
    public static function GetOne($sqlQuery, $params = null) 
    { 
    // Initialize the return value to null  
    $result = null; 

    // Try to execute an SQL query or a stored procedure 
    try 
    { 
     $statement_handler = self::Prepare($sqlQuery); 

     // Execute the query 
     $statement_handler->execute($params); 

     // Fetch result 
     $result = $statement_handler->fetch(PDO::FETCH_NUM); 

     /* Save the first value of the result set (first column of the first row) 
     to $result */ 
     $result = $result[0]; 
    } 
    // Trigger an error if an exception was thrown when executing the SQL query 
    catch(PDOException $e) 
    { 
     // Close the database handler and trigger an error 
     self::Close(); 
     trigger_error($e->getMessage(), E_USER_ERROR); 
    } 

    // Return the query results 
    return $result; 
    } 
} 
?> 
0

你應該通過一個連接對象(可能PDO)左右,而在不同的地方應該能夠挑選起來,無論是作爲一個參數,或一些中央對象,該有的都有了一個參考的屬性到,或者什麼的。

有多個連接可能會有用,似乎瘋狂的是,當你想要一個新的連接時,mysql_connect選擇一個現有的連接 - 但它無論如何都是瘋了。只需使用PDO。

2

我認爲最好的方法是使用特殊的類來處理mysql連接並將其用作單例。使構造函數保持私有狀態並使其返回現有連接或新連接的實例。

這裏是我的例子:

class db 
{ 

    public $host; 
    public $user; 
    public $pass; 
    public $database; 

    private static $instance = false; 

    private function __construct() 
    { 

    } 

    public static function getInstance() 
    { 
     if (self::$instance === false) 
     { 
      self::$instance = new db; 
     } 
     return self::$instance; 
    } 

     public function db_connect() 
     { 
     } 

     public function db_disconnect() 
     { 
     } 
} 

這樣一來,只要您撥打:DB ::的getInstance() - > db_connect(),你一定有唯一的將是無處不在這一方面的一個實例。

+0

我現在只是回顧一下,以供參考。我有點困惑,你有它作爲db :: getInstance() - > db_connect()我需要使用db :: getInstance() - > METHODNAME - 這裏爲我在DB類調用的每個方法?如果我有一個名爲execute()的方法,我也會傳遞一個SQL查詢,我需要這樣做,db :: getInstance() - > execute($ sql)?謝謝您的幫助 – JasonDavis 2010-01-14 23:52:46

相關問題