略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: mysql_list_dbs

2024-04-28

mysql_list_dbs

(PHP 4, PHP 5)

mysql_list_dbs 列出 MySQL 服务器中所有的数据库

说明

mysql_list_dbs(resource $link_identifier = ?): resource

mysql_list_dbs() 将返回一个结果指针,包含了当前 MySQL 进程中所有可用的数据库。用 mysql_tablename() 函数来遍历此结果指针,或者任何使用结果表的函数,例如 mysql_fetch_array()

示例 #1 mysql_list_dbs() 例子

<?php
$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_fetch_row() 或类似函数一样简单。

为向下兼容仍然可以使用 mysql_listdbs(),但反对这样做。

参见 mysql_db_name()

add a noteadd a note

User Contributed Notes 4 notes

up
2
jeff at forerunnertv dot com
2 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
-1
matjung at hotmail dot com
12 years ago
The result pointer contains only the databases for which the mysql_user has the select priviledge granted.
up
-2
busilezas at gmail dot com
7 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
-7
theriault
9 years ago
Another alternative to this function is:

SQL Query: SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA

官方地址:https://www.php.net/manual/en/function.mysql-list-dbs.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3