多資料庫連線

1 篇文章 / 0 new
author
多資料庫連線
setting.php 內各資料庫的連線設定
    $databases = array(
        'default' => //預設資料庫
        array(
            'default' => array(
                'driver' => 'mysql',
                'database' => 'd7',
                'username' => 'username',
                'password' => 'userpassword',
                'host' => 'localhost',
                'port' => '',
                'prefix' => '',
            ),
        ),
        'otherdb' => // 指定資料庫, 代號 otherdb
        array(
            'default' => array(
                'driver' => 'mysql',
                'database' => 'legacydatabase',
                'username' => 'legacyusername',
                'password' => 'legacyuserpassword',
                'host' => '122.185.22.1',
                'port' => '6060',
            ),
        ),
    );
► 連線資料庫之切換 db_set_active
// 切換到指定資料庫
db_set_active('otherdb');
$result = db_query("SELECT * FROM ldap_user WHERE uid = :uid", array(':uid' => $user->uid));
 
// 區回預設連線
db_set_active('default');
Free Web Hosting