我可以显示所有数据库,我可以显示特定数据库的所有表,但我想在php中创建一个循环或foreach以获取mysql中的所有数据库,foreach数据库显示链接到数据库名称的所有表。我想要一个像这样的阵列...... 数组([database_name] = employees,[tables] = name,surame)只是一个例子
我试过
$db = new MysqliDb ('');
$All_databases=$db->rawQuery('SHOW DATABASES'); // this gets all databases
foreach ($All_databases as $key => $value) {
$AllDbs[] = $value['Database']; // array of all databases found
$dbname=$value['Database']; // assign $dbname as a database name
foreach ($AllDbs as $tbl) { // foreach database found find its tables
$db2 = new MysqliDb ($dbname);
$db_tables=$db2->rawQuery('SHOW TABLES');
foreach ($db_tables as $tbl) {
$table_name=$tbl['Tables_in_'.$dbname.''];
}
}
}
答案 0 :(得分:1)
使用information_schema表来查询此类信息要简单得多,特别是SCHEMATA和TABLES表对您有用。
要在单个查询中获得所需内容,只需在SCHEMATA上连接TABLES并使用group_concat()生成以逗号分隔的表列表:
select s.SCHEMA_NAME, group_concat(t.TABLE_NAME)
from information_schema.SCHEMATA s
left join information_schema.TABLES t on s.SCHEMA_NAME=t.TABLE_SCHEMA
group by s.SCHEMA_NAME