2011-05-08 181 views
1

我有一個簡單的PHP/MySql應用程序,它通常會選擇幾個數據庫中的一個(讓我們說每個客戶)來操作。但是,頻繁地調用訪問公共數據庫的效用函數。Push/pop當前數據庫

我不想在我的代碼灑USE條款,所以它看起來像我應該在每一個效用函數開始推動當前數據庫,並在結束時再次彈出它。像這樣的事情(從我的頭頂開始,如此大膽地行不通,但會給出一個想法)。

function ConnectToDatabase($db) 
{ 
    global $current_database; 
    $current_database = $db; 
    odb_exec('USE ' . $db); // etc. Error handling omitted for clarity 
} 

function UtilityFunction() 
{ 
    odb_exec('USE common_db'); // etc. Error handling omitted for clarity 
    // do some work here 
    global $current_database; 
    ConnectToDatabase($current_database); 
} 

也許我可以讓它更漂亮結合global $current_database; ConnectToDatabase($current_database);PopCurrentDb功能,但你得到的圖片。

這是更好地在PHP中完成?有沒有MySql解決方案(但後來我想要符合ODBC,所以也許PHP更好)。別人怎麼做?


更新:到底我決定總是完全有資格獲得,
例如SELECT * from $database . '.' . $table

+2

您可以打開兩個數據庫連接,你知道嗎? :) – deceze 2011-05-08 00:12:33

+1

mysql_select_db選擇數據庫我猜 – Ibu 2011-05-08 00:14:07

+0

+1是的,我想打開第二個連接是一種選擇。只保留一個很簡單,它不是一個非常複雜的程序。 – Mawg 2011-05-08 00:26:39

回答

4

你爲什麼不只是做出某種數據庫管理器類的,只是推說身邊?將所有數據庫名稱/連接存儲集中在一個實體中。這樣你就有一個明確的api來訪問它,你可以按名稱使用db。

class MultiDb 
{ 

    /* 
    * Array of PDO DB objects or PDO DSN strings indexed by a connection/dbname name 
    * 
    * @var array 
    */ 
    protected $connections = array(); 

    /* 
    * The connection name currently in use 
    * @var string 
    */ 
    protected $currentConnection; 

    /* 
    * The Defualt connection name 
    * 
    * @var string 
    */ 
    protected $defaultConncetion; 

    /* 
    * @param array $connections Any array DSN or PDO objects 
    */ 
    public function __construct(array $connections); 

    public function getConnection($name); 

    // i would set this up to intelligently return registered connections 
    // if the argument matches one 
    public function __get($name) 

    // same with __set as with __get 
    public function __set($name, $value); 

    // proxy to the current connection automagically 
    // if current isnt set yet then use default so things 
    // running through this would actually result in 
    // call_user_func_array(array(PDO $object, $method), $args); 

    public function __call($method, $args); 

} 

所以使用可能看起來像

// at the beginning of the app 

$db = new MultiDb(array(
    'util' => array('mysql:host=localhost;dbname=util;', 'user', 'pass'); 
    'default' => array('odbc:DSN=MYDSN;UID=user;PWD=pass;'); 
)); 


// some where else in the app we want to get some ids of some entities and then 
// we want to delete the associated logs in our shared utility DB 

// fetch the ids from the default db 

$ids = $db->default->query('SELECT c.name, c.id FROM some_table c') 
    ->fetchAll(PDO::FETCH_KEY_PAIR); 

// assume we have written a method 
// to help us create WHERE IN clauses and other things 
$in = $db->createQueryPart($ids, MultiDb::Q_WHERE_IN); 

// prepare our delete from the utility DB 
$stmt = $db->util->prepare(
    'DELETE FROM log_table WHERE id IN('.$in['placeholder'].')', 
    $in['params'] 
); 

// execute our deletion 
$stmt->execute(); 
+0

+1謝謝。好的代碼 – Mawg 2011-05-11 04:53:23

1

所以你想創建一個功能推(插入)和彈出(選擇&刪除)? 您可以創建一個存儲過程來處理這個問題,或者您可以在php中編寫多個查詢執行。