8

I was wondering if there's a way in PHP to list all available databases by usage of mysqli. The following works smooth in MySQL (see php docs):

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$db_list = mysql_list_dbs($link);

while ($row = mysql_fetch_object($db_list)) {
 echo $row->Database . "\n";
}  

Can I Change:

$db_list = mysql_list_dbs($link); // mysql

Into something like:

$db_list = mysqli_list_dbs($link); // mysqli

If this is not working, would it be possible to convert a created mysqli connection into a regular mysql and continue fetching/querying on the new converted connection?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Apologies. Using mysqli you can run "show databases" instead. –  Jan 13 '11 at 06:10
  • If anyone is thinking of using `mysql_list_dbs`, it will stop working in the future as it is **deprecated**. More info at http://php.net/manual/en/function.mysql-list-dbs.php . So better to use the code from one of the answers, below. – ban-geoengineering Jun 18 '18 at 13:43

4 Answers4

13

It doesn't appear as though there's a function available to do this, but you can execute a show databases; query and the rows returned will be the databases available.

EXAMPLE:

Replace this:

$db_list = mysql_list_dbs($link); //mysql 

With this:

$db_list = mysqli_query($link, "SHOW DATABASES"); //mysqli
jsherk
  • 6,128
  • 8
  • 51
  • 83
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
7

I realize this is an old thread but, searching the 'net still doesn't seem to help. Here's my solution;

$sql="SHOW DATABASES";
$link = mysqli_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql: ' . mysqli_error($link).'\r\n');

if (!($result=mysqli_query($link,$sql))) {
        printf("Error: %s\n", mysqli_error($link));
    }

while( $row = mysqli_fetch_row( $result ) ){
        if (($row[0]!="information_schema") && ($row[0]!="mysql")) {
            echo $row[0]."\r\n";
        }
    }
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
Rick
  • 101
  • 1
  • 2
  • 4
    @DenizZoeteman It wasn't necessary for you to comment on a post that directly answers the question regardless of the age. It helped me and that completely negates your point. Yeah, the same info is elsewhere on the internet, but it's not always presented the right way for everyone to understand. Some people and their high horses... – Anthony Jan 20 '16 at 18:27
  • I have this code to get database list but it is returning only one schema "information_schema", why not all databases? Please help – Sandeep Feb 02 '17 at 06:46
  • You have a mistake in your code. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Dec 28 '19 at 13:59
2

Similar to Rick's answer, but this is the way to do it if you prefer to use mysqli in object-orientated fashion:

$mysqli = ... // This object is my equivalent of Rick's $link object.

$sql = "SHOW DATABASES";
$result = $mysqli->query($sql);
if ($result === false) {
    throw new Exception("Could not execute query: " . $mysqli->error);
}

$db_names = array();
while($row = $result->fetch_array(MYSQLI_NUM)) { // for each row of the resultset
    $db_names[] = $row[0]; // Add db name to $db_names array
}

echo "Database names: " . PHP_EOL . print_r($db_names, TRUE); // display array
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • Relevant: [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Dec 28 '19 at 13:58
0

Here is a complete and extended solution for the answer, there are some databases that you do not need to read because those databases are system databases and we do not want them to appear on our result set, these system databases differ by the setup you have in your SQL so this solution will help in any kind of situations.

first you have to make database connection in OOP

//error reporting Procedural way
//mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

//error reporting OOP way
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL & MYSQLI_REPORT_STRICT;

$conn = new mysqli("localhost","root","kasun12345");

using Index array of search result

$dbtoSkip = array("information_schema","mysql","performance_schema","sys");

$result = $conn->query("show databases");
while($row = $result->fetch_array(MYSQLI_NUM)){
    $print = true;

    foreach($dbtoSkip as $key=>$vlue){
        if($row[0] == $vlue) {
            $print=false;
            unset($dbtoSkip[$key]);
        }
    }

    if($print){
        echo '<br/>'.$row[0];
    }
}   

same with Assoc array of search result

$dbtoSkip = array("information_schema","mysql","performance_schema","sys"); 

$result = $conn->query("show databases");
while($row = $result->fetch_array(MYSQLI_ASSOC)){
    $print = true;

    foreach($dbtoSkip as $key=>$vlue){
        if($row["Database"] == $vlue) {
            $print=false;
            unset($dbtoSkip[$key]);
        }
    }

    if($print){
        echo '<br/>'.$row["Database"]; 
    }
}

same using object of search result

$dbtoSkip = array("information_schema","mysql","performance_schema","sys"); 

$result = $conn->query("show databases");
while($obj = $result->fetch_object()){
    $print = true;
    foreach($dbtoSkip as $key=>$vlue){
        if( $obj->Database == $vlue) {
            $print=false;
            unset($dbtoSkip[$key]);
        }
    }

    if($print){
        echo '<br/>'. $obj->Database;
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aylian Craspa
  • 422
  • 5
  • 11
  • 1
    You have a mistake in your first code example. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Dec 28 '19 at 13:56
  • thanks i think now its oki, if its still not in the correct way(as you think) please post the code correction. – Aylian Craspa Dec 30 '19 at 06:48