PHP MySQL - 列出除 information_schema、mysql 之外的所有数据库

Apr*_*cot 2 php mysql

在我的网页中,我想通过 PHP 列出 mysql 中可用的所有数据库。

以下代码列出了所有数据库:

<?php
$link = mysql_connect('localhost', 'root', 'pass123');
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['Database'] . "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)

但是,我想'information_schema', 'mysql' and 'performance_schema'从数据库列表中排除。

在 mysql 终端,我尝试了:

show schema_name as database from information_schema.SCHEMATA where schema_name NOT IN ('information_schema','mysql');
Run Code Online (Sandbox Code Playgroud)

但出现错误...未知的列名 schema_name。

Man*_*n S 5

只需从 php 端排除,如下所示。

$link = mysql_connect('localhost', 'root', 'pass123');
$res = mysql_query("SHOW DATABASES");

$exclude_db = array('information_schema', 'mysql', 'performance_schema');
while ($row = mysql_fetch_assoc($res)) {
    if(!in_array($row['Database'], $exclude_db)){
        echo $row['Database'] . "<br />\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我们也可以在查询本身中排除,如下所示。

SELECT `schema_name` from INFORMATION_SCHEMA.SCHEMATA  WHERE `schema_name` NOT IN('information_schema', 'mysql', 'performance_schema');
Run Code Online (Sandbox Code Playgroud)