PHP 8.3.4 Released!

mysql_list_dbs

(PHP 4, PHP 5)

mysql_list_dbs列出 MySQL 服务器中可用的数据库

警告

本函数自 PHP 5.4.0 起已废弃,并且它和整个MySQL 扩展自 PHP 7.0.0 开始被移除。 可以选择出于活跃开发中的MySQLiPDO_MySQL扩展来作为替代。 参见MySQL:选择 API 指南来获取更多信息。 用以替代本函数的有:

  • SQL 查询:SHOW DATABASES

说明

mysql_list_dbs(resource $link_identifier = NULL): resource

返回结果指针,包含当前 MySQL 守护进程中可用的数据库。

参数

link_identifier

MySQL 连接。如不指定连接标识,则使用由 mysql_connect() 最近打开的连接。如果没有找到该连接,会尝试不带参数调用 mysql_connect() 来创建。如没有找到连接或无法建立连接,则会生成 E_WARNING 级别的错误。

返回值

Returns a result pointer resource on success, or false on failure. 使用 mysql_tablename() 函数或任何使用结果表的函数(比如 mysql_fetch_array())来遍历此结果指针。

示例

示例 #1 mysql_list_dbs() 示例

<?php
// Usage without mysql_list_dbs()
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$res = mysql_query("SHOW DATABASES");

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

// Deprecated as of PHP 5.4.0
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$db_list = mysql_list_dbs($link);

while (
$row = mysql_fetch_object($db_list)) {
echo
$row->Database . "\n";
}
?>

以上示例的输出类似于:

database1
database2
database3

注释

注意:

为了向下兼容,可以使用下列已废弃的别名: mysql_listdbs()

参见

add a note

User Contributed Notes 4 notes

up
1
jeff at forerunnertv dot com
3 years ago
There is no direct equivalent of mysql_list_dbs() as a mysqli_list_dbs() command, but you can query "show databases" instead.

So this:

$db_list = mysql_list_dbs($connect); //mysql

Is equivalent to this:

$db_list = mysqli_query($connect, "SHOW DATABASES"); //mysqli
up
-2
busilezas at gmail dot com
9 years ago
The example is wrong in Spanish version.

ERROR: mysql_fetch_assoc() expects parameter 1 to be resource, null given in XXX on line 5
while ($fila = mysql_fetch_assoc($res)) {

OK.
while ($fila = mysql_fetch_assoc($resultado)) {
up
-2
matjung at hotmail dot com
14 years ago
The result pointer contains only the databases for which the mysql_user has the select priviledge granted.
up
-10
theriault
11 years ago
Another alternative to this function is:

SQL Query: SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
To Top