PDO code to show all databases

PDO code to show all databases

Showing a list of all the databases using PHP and PDO.

<?php 
$user = 'root';
$pass = 'password';
$server = 'localhost';

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

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


Will out a list of databases on localhost with the username:root and password:password

If you would like to get this data and put it into a table its quite simple.


<?php 
$user = 'root';
$pass = 'password';
$server = 'localhost';

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



<?php while( ( $db = $dbs->fetchColumn( 0 ) ) !== false ) : ?>

<table><td> <?php echo $db ; ?> </td></table>

 <?php endwhile; ?>

Leave a Reply

Be the First to Comment!

Notify of

wpDiscuz