how to get array of databases in localhost using PDO

qadenza :

trying to get an array of databases in localhost using PDO

$user = 'root';
$server = 'localhost';
$db = new PDO("mysql:host=$server", $user);

$sql = "show databases";
$st = $db->prepare($sql);
$st->execute();
$arr = $st->fetchAll(PDO::FETCH_ASSOC);
foreach($arr as $el){
    echo $el . '<br>'; // error - array to string conversion
}

also tried:

 $arr = $st->fetch(PDO::FETCH_ASSOC);

result - only the first database is echoed

Any help?

Bernd Buffen :

Get the Information from the information schema like:

SELECT `SCHEMA_NAME` FROM `information_schema`.`SCHEMATA`;

There are also more infos about the databases (schemas)

sample

MariaDB [(none)]> SELECT `SCHEMA_NAME` FROM `information_schema`.`SCHEMATA`;
+--------------------+
| SCHEMA_NAME        |
+--------------------+
| bernd              |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170194&siteId=1