mysql_list_dbs

(PHP 4, PHP 5)

mysql_list_dbsList databases available on a MySQL server

Warning

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

  • SQL Query: SHOW DATABASES

說明

resource mysql_list_dbs ([ resource $link_identifier = NULL ] )

Returns a result pointer containing the databases available from the current mysql daemon.

Warning

自PHP 5.4.0起,此函式已經被廢棄。強烈建議不要使用此函式。

參數

link_identifier

MySQL 的連接識別符。如果沒有指定,預設使用最後被 mysql_connect() 打開的連接。如果沒有找到該連接,函式會嘗試呼叫 mysql_connect() 建立連接並使用它。如果發生意外,沒有找到連接或無法建立連接,系統發出 E_WARNING 級別的警告信息。

回傳值

Returns a result pointer resource on success, or FALSE on failure. Use the mysql_tablename() function to traverse this result pointer, or any function for result tables, such as mysql_fetch_array().

範例

Example #1 mysql_list_dbs() example

<?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

註釋

Note:

為了保證向前相容,可以使用下面的別名,但不贊成使用它: mysql_listdbs()

參見

add a note add a note

User Contributed Notes 4 notes

up
2
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
-1
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
-1
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
-4
theriault
11 years ago
Another alternative to this function is:

SQL Query: SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
To Top