How to determine which database is selected

Mysql

Mysql Problem Overview


After calling mysql_select_db to grab a database, is there any way to later output the name of the database that is currently selected? This seems very basic but I couldn't find anything on php.net or stackoverflow (all results are for "no database selected").

Mysql Solutions


Solution 1 - Mysql

Just use mysql_query (or mysqli_query, even better, or use PDO, best of all) with:

SELECT DATABASE() FROM DUAL;

Addendum:

There is much discussion over whether or not FROM DUAL should be included in this or not. On a technical level, it is a holdover from Oracle and can safely be removed. If you are inclined, you can use the following instead:

SELECT DATABASE();

That said, it is perhaps important to note, that while FROM DUAL does not actually do anything, it is valid MySQL syntax. From a strict perspective, including braces in a single line conditional in JavaScript also does not do anything, but it is still a valid practice.

Solution 2 - Mysql

SELECT DATABASE();

p.s. I didn't want to take the liberty of modifying @cwallenpoole's answer to reflect the fact that this is a MySQL question and not an Oracle question and doesn't need DUAL.

Solution 3 - Mysql

You can always use STATUS command to get to know Current database & Current User

enter image description here

Solution 4 - Mysql

In the comments of http://www.php.net/manual/de/function.mysql-db-name.php I found this one from ericpp % bigfoot.com:

If you just need the current database name, you can use MySQL's SELECT DATABASE() command:

<?php
function mysql_current_db() {
    $r = mysql_query("SELECT DATABASE()") or die(mysql_error());
    return mysql_result($r,0);
}
?>

Solution 5 - Mysql

SELECT DATABASE() worked in PHPMyAdmin.

Solution 6 - Mysql

Another way for filtering the database with specific word.

SHOW DATABASES WHERE `Database` LIKE '<yourDatabasePrefixHere>%'
or
SHOW DATABASES LIKE '<yourDatabasePrefixHere>%';

Example:

SHOW DATABASES WHERE `Database` LIKE 'foobar%'
foobar_animal
foobar_humans_gender
foobar_objects

Solution 7 - Mysql

@mysql_result(mysql_query("SELECT DATABASE();"),0)

If no database selected, or there is no connection it returns NULL otherwise the name of the selected database.

Solution 8 - Mysql

slightly off-topic (using the CLI instead of PHP), but still worth knowing: You can set the prompt to display the default database by using any of the following

mysql --prompt='\d> '
export MYSQL_PS1='\d> '

or once inside

prompt \d>\_
\R \d>\_

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandrewtweberView Question on Stackoverflow
Solution 1 - MysqlcwallenpooleView Answer on Stackoverflow
Solution 2 - MysqlElijah LynnView Answer on Stackoverflow
Solution 3 - MysqlMogliView Answer on Stackoverflow
Solution 4 - MysqlJan ThomäView Answer on Stackoverflow
Solution 5 - MysqlAbhi ChavdaView Answer on Stackoverflow
Solution 6 - MysqlDexterView Answer on Stackoverflow
Solution 7 - Mysqluser669677View Answer on Stackoverflow
Solution 8 - MysqlJeffView Answer on Stackoverflow