MySQL query : SHOW DATABASES
which return all database as result
as you use mysqli to get table 's data , in same manor you can use for SHOW DATABASES
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if ($result = $mysqli->query("SHOW DATABASES")) {
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
echo $row ."\n";
}
// Free result set
$result->close();
}
$mysqli->close();
?>
Above code will result database list
or if you using older version of php than
$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_assoc($res)) {
echo $row['Database'] . "\n";
}
=== other way ===
one another long query which also result all database name
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
here SCHEMA_NAME is the database name
MySQL store all meta data(data about data) at INFORMATION_SCHEMA
INFORMATION_SCHEMA database have SCHEMATA table , which have following column
CATALOG_NAME
SCHEMA_NAME
DEFAULT_CHARACTER_SET_NAME
DEFAULT_COLLATION_NAME
SQL_PATH
its all about database . it store database 's character set , collation name and sql path and database 's category .
PHP code to get database list from INFORMATION_SCHEMA database
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if ($result = $mysqli->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA")) {
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
echo $row ."\n";
}
// Free result set
$result->close();
}
$mysqli->close();
?>
if you are using older version of php than
$res = mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA");
while ($row = mysql_fetch_assoc($res)) {
echo $row['SCHEMA_NAME'] . "\n";
}