andrewtweber
andrewtweber

Reputation: 25579

How to determine which database is selected

Is there any way to later output the name of the database that is currently selected?

Upvotes: 333

Views: 309245

Answers (5)

Dexter
Dexter

Reputation: 9344

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

Upvotes: 4

cwallenpoole
cwallenpoole

Reputation: 82068

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

SELECT DATABASE();

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 it as follows:

SELECT DATABASE() FROM DUAL;

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.

Upvotes: 469

Jeff
Jeff

Reputation: 2245

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>\_

Upvotes: 7

Mogli
Mogli

Reputation: 760

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

enter image description here

Upvotes: 71

Elijah Lynn
Elijah Lynn

Reputation: 13528

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.

Upvotes: 130

Related Questions