2011-05-08 173 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中编写多个查询执行。