5

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

donohoe
  • 13,867
  • 4
  • 37
  • 59
homerun
  • 19,837
  • 15
  • 45
  • 70

6 Answers6

16
$result = mysqli_query($db_conn,"SHOW DATABASES"); 
while ($row = mysqli_fetch_array($result)) { 
    echo $row[0]."<br>"; 
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 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
  • 14,307
  • 5
  • 43
  • 55
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,738
  • 21
  • 105
  • 131
jjohn
  • 9,708
  • 4
  • 20
  • 21
-2

The MySQL command for this is

SHOW DATABASES

See the manual for more info on the SHOW command

liquorvicar
  • 6,081
  • 1
  • 16
  • 21
-2

Write the SQL query:

  show databases
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kasavbere
  • 5,873
  • 14
  • 49
  • 72
-2

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

NewUser
  • 12,713
  • 39
  • 142
  • 236