5

How can I get a list of all the MySQL databases that exist on a server using PHP?

donohoe
  • 13,411
  • 4
  • 35
  • 56
homerun
  • 18,461
  • 14
  • 42
  • 70

6 Answers6

16
$result = mysqli_query($db_conn,"SHOW DATABASES"); 
while ($row = mysqli_fetch_array($result)) { 
    echo $row[0]."<br>"; 
}
Dharman
  • 26,923
  • 21
  • 73
  • 125
  • 1
    ^^ thumbs up. for mysqli.. `$result = mysqli_query($db_conn,"SHOW DATABASES"); while ($row = mysqli_fetch_array($result)) { echo $row[0]."
    "; }`
    – James Walker Jan 28 '18 at 21:40
1
$dbcnx = mysql_connect ($dbhost, $dbusername, $dbpassword); 
$result = @mysql_query('SHOW DATABASES'); 

while ($row = mysql_fetch_array($result)) { 
 print_r ($row)
} 
Tamik Soziev
  • 13,578
  • 5
  • 40
  • 54
0

At the MySQL prompt, SHOW DATABASES does what you want.

You can run this command as a query from PDO or the native PHP MySQL library and read the returned rows. Pretend it is a normal select.

You will only see the databases that the account used to connected to MySQL can see.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
jjohn
  • 9,130
  • 4
  • 22
  • 21
-2

The MySQL command for this is

SHOW DATABASES

See the manual for more info on the SHOW command

liquorvicar
  • 5,903
  • 1
  • 14
  • 20
-2

Write the SQL query:

  show databases
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
kasavbere
  • 5,721
  • 10
  • 48
  • 72
-2

Just use SHOW DATABASES.It will show all the databases present in your MySQL.

NewUser
  • 12,175
  • 36
  • 139
  • 230