如何使用PDO在PHP中获取MySQL数据库列表?

时间:2022-06-05 01:11:39

I wonder how can I get the list of MySQL databases in PHP using PDO without having to connect to a database first ( I mean no dbname in dsn )?

我想知道如何使用PDO在PHP中获取MySQL数据库列表而不必先连接到数据库(我的意思是在dsn中没有dbname)?

Usually I used to use the function mysql_list_dbs() but I no longer use mysql this way.

通常我以前使用函数mysql_list_dbs()但我不再用这种方式使用mysql。

5 个解决方案

#1


12  

You can use

您可以使用

show databases

or a query on the information_schema:

或者对information_schema的查询:

select schema_name from information_schema.schemata

#2


14  

Thanks nick rulez. I made an example of DBs listing:

谢谢nick rulez。我做了一个DBs列表的例子:

$user = 'root';
$pass = 'root';
$server = 'localhost';

$dbh = new PDO( "mysql:host=$server", $user, $pass );
$dbs = $dbh->query( 'SHOW DATABASES' );

while( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
{
    echo $db.'<br>';
}

#3


0  

Another method similar to Falcon's:

另一种类似Falcon的方法:

This script uses foreach instead of while and print instead of echo for the db names and the break tag is set up as it would be used with XML. It also uses the associative property, the column name, instead of the array index of the column in the returned row to display the desired result.

此脚本使用foreach而不是while而打印而不是echo作为db名称,并设置break标记,因为它将与XML一起使用。它还使用关联属性(列名),而不是返回行中列的数组索引来显示所需的结果。

This assumes you have the PDO connection properly set up and = $dbconn. Many times $db is used instead of $dbconn in examples.

假设您正确设置了PDO连接并且= $ dbconn。在示例中,很多时候使用$ db而不是$ dbconn。

$stmt ="SHOW DATABASES";
foreach($dbconn->query($stmt) as $row){
print $row['Database'];echo"<br />";
}

#4


0  

try{
  $DBH = new PDO("mysql:host=localhost", "root", "");
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  $DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch(PDOException $e) {
  echo "Fail";
}

$rs = $dbo->query("SHOW DATABASES");
while ($h = $rs->fetch(PDO::FETCH_NUM)) {
   echo $r[0]."<br>";
}

#5


-6  

In PHP version 5.4.0 you can use mysql_list_dbs() command:

在PHP 5.4.0版中,您可以使用mysql_list_dbs()命令:

$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";
}

#1


12  

You can use

您可以使用

show databases

or a query on the information_schema:

或者对information_schema的查询:

select schema_name from information_schema.schemata

#2


14  

Thanks nick rulez. I made an example of DBs listing:

谢谢nick rulez。我做了一个DBs列表的例子:

$user = 'root';
$pass = 'root';
$server = 'localhost';

$dbh = new PDO( "mysql:host=$server", $user, $pass );
$dbs = $dbh->query( 'SHOW DATABASES' );

while( ( $db = $dbs->fetchColumn( 0 ) ) !== false )
{
    echo $db.'<br>';
}

#3


0  

Another method similar to Falcon's:

另一种类似Falcon的方法:

This script uses foreach instead of while and print instead of echo for the db names and the break tag is set up as it would be used with XML. It also uses the associative property, the column name, instead of the array index of the column in the returned row to display the desired result.

此脚本使用foreach而不是while而打印而不是echo作为db名称,并设置break标记,因为它将与XML一起使用。它还使用关联属性(列名),而不是返回行中列的数组索引来显示所需的结果。

This assumes you have the PDO connection properly set up and = $dbconn. Many times $db is used instead of $dbconn in examples.

假设您正确设置了PDO连接并且= $ dbconn。在示例中,很多时候使用$ db而不是$ dbconn。

$stmt ="SHOW DATABASES";
foreach($dbconn->query($stmt) as $row){
print $row['Database'];echo"<br />";
}

#4


0  

try{
  $DBH = new PDO("mysql:host=localhost", "root", "");
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  $DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch(PDOException $e) {
  echo "Fail";
}

$rs = $dbo->query("SHOW DATABASES");
while ($h = $rs->fetch(PDO::FETCH_NUM)) {
   echo $r[0]."<br>";
}

#5


-6  

In PHP version 5.4.0 you can use mysql_list_dbs() command:

在PHP 5.4.0版中,您可以使用mysql_list_dbs()命令:

$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";
}