mysql_db_name Function in PHP

What is mysql_db_name() function in PHP?
How does mysql_db_name works?


mysql_db_name() function retrieve the database name from a call to mysql_list_dbs().
string mysql_db_name ( resource result, int row [, mixed field])

Returns the database name on success, and FALSE on failure.
mysql_db_name() takes the result pointer from a call to mysql_list_dbs() as its first parameter and the second parameter 'row' is an index of the result set.
By using mysql_list_dbs() function we can get the list of database name exist in the server as a result set, by using a pointer to this result set we can get the list of all the database names.

or die ("Could not connect");
//retrieve list of existing database in the server
$db_list = mysql_list_dbs($link);
$i = 0;
//count the number of database in the list
$cnt = mysql_num_rows($db_list);
while ($i < $cnt) {
//display database name
echo mysql_db_name($db_list, $i) . "n";

In the above code list of available databases in the server is fetched as a resultset. This resultset and index is passed as a parameter to mysql_db_name function to retrieve the database name of passed index. Index ranges from '0' to the end of the resultset returned by mysql_list_dbs() function.

PHP Topics

Ask Questions

Ask Question