ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

List all database names in MySQL


<?php
//Connect to the MySQL server using PDO.
$pdo = new PDO("mysql:host=localhost", "root", "");

//Execute a "SHOW DATABASES" SQL query.
$stmt = $pdo->query('SHOW DATABASES');

//Fetch the columns from the returned PDOStatement
$databases = $stmt->fetchAll(PDO::FETCH_COLUMN);

//Loop through the database list and print it out.
foreach($databases as $database){
//$database will contain the database name
//in a string format
echo $database, '<br>';
}
?>

Categroy:
PHP