|
Home >> FAQs/Tutorials >> MySQL Tutorials
MySQL Tutorial - List All Existing Databases
By: FYIcenter.com
(Continued from previous topic...)
How To Get a List of Databases from MySQL Servers?
Once you got a MySQL server connection object successfully, you need
to know what databases are available on the server and which database you
should use to manage your tables and data. To get a list of all available
databases on your MySQL server, you can use the mysql_list_dbs() function.
Note mysql_list_dbs() will not return an array of database names.
It returns a result object, which requires mysql_fetch_object() call to
loop through each database object.
The tutorial exercise below shows you a good example of how to get a list
of databases as a result object. Then "while" loop is used to fetch each
database object from the result object. Finally, an object property "$obj->Database"
is used to get the database name:
<?php
$con = mysql_connect('localhost:8888', 'dev', 'iyf');
print("List of databases:\n");
$res = mysql_list_dbs($con);
while ($obj = mysql_fetch_object($res)) {
print($obj->Database . "\n");
}
mysql_close($con);
?>
If you run this script, you will get something like this:
List of databases:
information_schema
mysql
test
(Continued on next topic...)
- How To Install PHP on Windows?
- How To Verify Your PHP Installation?
- What Do You Need to Connect PHP to MySQL?
- How To Turn on mysql Extension on the PHP Engine?
- How To Connect to a MySQL Server with Default Port Number?
- How To Connect to a MySQL Server with a Port Number?
- How To Connect to MySQL Server with User Accounts?
- How To Access MySQL Servers through Firewalls?
- How To Get Some Basic Information Back from MySQL Servers?
- How To Close MySQL Connection Objects?
- How To Get a List of Databases from MySQL Servers?
- How To Create a New Database?
- What Happens If You Do Not Have Privileges to Create Database?
- How To Get MySQL Statement Execution Errors?
- How To Drop an Existing Database?
- How To Select an Exiting Database?
- Can You Select Someone Else Database?
- How To Execute a SQL Statement?
|