2010-10-26 66 views
1

我如何使用單獨的密碼登錄數據庫?我們只能在database.php中配置一個用戶。我希望登錄過程應該發生在不同的mysql用戶名和密碼上,因爲這個表是受保護的。如何使用單獨的密碼登錄數據庫?

感謝提前:)

+0

是否使用的是RDBMS? – Victor 2010-10-26 06:14:20

回答

2

通過下面的用戶文檔,你會發現這個頁面: http://codeigniter.com/user_guide/database/connecting.html

文檔指出您可以通過指定一組連接到多個數據庫。 如果您在database.php中的文件看,你會看到該連接陣列的格式是這樣的:

$db['default']['hostname'] = "localhost"; 

的「組」在這裏是默認,這是載入你一直一直的方式做:

$this->load->database(); 

當你需要連接到另一個數據庫,指定一個新的組:

$db['my_secret_db']['hostname'] = "localhost"; 
$db['my_secret_db']['username'] = "other_mysql_user"; 
... 

你我加載t等這樣:

$MyOriginalDb = $this->load->database('default', true); 
$MyOtherDb = $this->load->database('my_secret_db', true); 

通過這些連接加載到自己的對象,你現在將使用:的

$MyOtherDb->query(); 

代替

$this->db->query(); 

我希望這有助於。

2

在database.php中創建另一個連接組

$active_group = "post"; 
$active_record = TRUE; 

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "user"; 
$db['default']['password'] = "user-only"; 
$db['default']['database'] = "something"; 
$db['default']['dbdriver'] = "mysql"; 
$db['default']['dbprefix'] = ""; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = TRUE; 
$db['default']['cache_on'] = FALSE; 
$db['default']['cachedir'] = ""; 
$db['default']['char_set'] = "utf8"; 
$db['default']['dbcollat'] = "utf8_general_ci"; 

$db['post']['hostname'] = "localhost"; 
$db['post']['username'] = "admin"; 
$db['post']['password'] = "admin-only"; 
$db['post']['database'] = "something"; 
$db['post']['dbdriver'] = "mysql"; 
$db['post']['dbprefix'] = ""; 
$db['post']['pconnect'] = TRUE; 
$db['post']['db_debug'] = TRUE; 
$db['post']['cache_on'] = FALSE; 
$db['post']['cachedir'] = ""; 
$db['post']['char_set'] = "utf8"; 
$db['post']['dbcollat'] = "utf8_general_ci"; 

創建另外兩個模型文件。一個文件加載'默認'配置,另一個加載'post'配置。

如:

$DB1 = $this->load->database('default', TRUE); 
$DB2 = $this->load->database('post', TRUE); 
相關問題