jack_of_all_trades
jack_of_all_trades

Reputation: 332

Can't select the current database name

I need to write a code that finds the name of the current sql database and puts that name in a variable. I'll take any suggestions, but what I was thinking was querying the current database name and putting that in a variable. I don't know if that will work because I can't seem to query my database name at all. The database I'm using for this is named "wp_plugin_development" but the query won't show me that. I'm using the code:

SELECT DB_NAME() AS [Current Database];

to get the database name in phpMyAdmin, but it brings up an error saying:

1305 - FUNCTION wp_plugin_development.DB_NAME does not exist

I don't know why it's doing that. Thanks for any help.

Upvotes: 0

Views: 973

Answers (4)

vrajesh
vrajesh

Reputation: 2942

try this:

global $wpdb;
echo $wpdb->dbname;

Upvotes: 1

Naveen Jackson
Naveen Jackson

Reputation: 11

SELECT schema();

can be used to retrieve the current db name in Mysql. this can also be assigned to a variable using AS keyword.

Upvotes: 1

Nico Weisenauer
Nico Weisenauer

Reputation: 284

If you are using MySQL try:

SELECT DATABASE();

or refer to this:

MySQL Doc: How to get DB Info

Upvotes: 3

rnevius
rnevius

Reputation: 27092

The WordPress database name is already stored as a constant in a standard wp-config.php. You can access it with DB_NAME.

<?php $database_name = DB_NAME; ?>

Upvotes: 2

Related Questions