Mitesh Hatkar
Mitesh Hatkar

Reputation: 59

list mysql databases using php

My phpmyadmin have these databases and looks like this:

demo_db_1
information_schema
mysql
performance_schema
demo_db_2
test_db_1
test_db_2

How can I get a list of all custom databases?

Meaning: when I run a file test.php, I would like to see a list like

demo_db_1
demo_db_1
test_db_1
test_db_2

I don't want to connect to any database. Just want the list of all the created databases. How can I achieve this?

Thanks in advance

Upvotes: 1

Views: 245

Answers (2)

Purushottam
Purushottam

Reputation: 844

You can use following code :

<?php
    //mysql_connect('HostName','UserName','Password');
    mysql_connect('localhost','root','');
    $database = mysql_query("show databases;");
    while($data = mysql_fetch_assoc($database))
    {
    $fetch[] = $data;
    }
    print "<pre>";
    print_r($fetch);
?>

Upvotes: 1

Mostafa Shahverdy
Mostafa Shahverdy

Reputation: 2725

Call a mysql query with mysql_query("show databases;");

You can get list of the databases you have access to with:

SHOW DATABASES;

If you want to get the list for some other user than the user you're logged in as, you have to query the mysql.db table.

Upvotes: 5

Related Questions