2010-09-26 30 views
0

我想列出所有的mysql數據庫及其各自的表,我目前使用這個,但任何人都可以推薦,如果有任何更好的方法。CodeIgniter使用多個數據庫

$q = $this->db->query('SHOW DATABASES'); 
$databases = $q->result_array(); 

foreach($databases as $db) { 
    $this->db->query('USE '. $db['Database']); 

    $q = $this->db->query('SHOW TABLES'); 
    $tables = $q->result_array();    
} 

回答

2

可以使用information_schema特殊的數據庫,裏面有說明所有其他數據庫,表和列的表。

這種方式,您只需要1個查詢:

SELECT table_schema, table_name 
FROM information_schema.tables 
ORDER BY table_schema, table_name; 
+0

我是一個學生,你能告訴我,這是否'information_schema'表適用於所有MySQL默認安裝?也可以使用v4 +或v5 +? – Shishant 2010-09-26 17:51:46

+0

@Shishant它只是從版本5.0.2開始添加到mysql:http://dev.mysql.com/tech-resources/articles/mysql-datadictionary.html - 在此之前,您應該使用'SHOW' 。 – Fanis 2010-09-26 18:01:18

+0

非常感謝 – Shishant 2010-09-26 18:10:22