简体   繁体   English

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

[英]PHP MySQL - List all databases except information_schema, mysql

In my webpage I want to list all the databases available in mysql through PHP. 在我的网页中,我想列出通过PHP在mysql中可用的所有数据库。

The following code lists all the databases: 以下代码列出了所有数据库:

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

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

However, I want to exclude 'information_schema', 'mysql' and 'performance_schema' from the list of databases. 但是,我想从数据库列表中排除'information_schema', 'mysql' and 'performance_schema'

At mysql terminal, I tried: 在mysql终端,我尝试了:

show schema_name as database from information_schema.SCHEMATA where schema_name NOT IN ('information_schema','mysql');

but getting errors....unknown column name schema_name. 但出现错误。...未知列名schema_name。

Just exclude from the php side like below. 只需从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";
    }
}

Edited: 编辑:

Also we can exclude in query itself like below. 我们也可以像下面这样排除查询本身。

SELECT `schema_name` from INFORMATION_SCHEMA.SCHEMATA  WHERE `schema_name` NOT IN('information_schema', 'mysql', 'performance_schema');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM