I want to get WordPress database name. I have also try to get database name from $wpdb but failed. When print the $wpdb it give an object array but i don't know how to get database name from object array.
2 Answers
To get the db name using $wpdb
:
global $wpdb;
echo $wpdb->dbname;
It will return database name as a string.
-
$wpdb->dbname
is a protected property: you shouldn't read it. UseDB_NAME
constant instead, as @chittaranjan suggested. Commented Feb 26, 2016 at 11:26 -
@AndreaSciamanna what is wrong using
$wpdb->dbname
? Would you please care to explain? That will be helpful.– тнє SufiCommented Feb 26, 2016 at 15:10 -
This is wrong answer! To use from
$dbname
you should call if at first byglobal $dbname
. Then no need towpdb
(it's wrong to usewpdb
). Just use$dbname
. But the better approach isDB_NAME
constant. Commented Feb 7, 2017 at 10:34 -
The constant DB_NAME is not always defined - when the HyperDB plugin is in use (replaces WPDB class to support multiple databases), then having a single DB_NAME constant wouldn't make sense– abhij89Commented Apr 8, 2021 at 11:32
You can also get the database name from the constant DB_NAME
without going for $wpdb
.
-
-
what scope do you need to be in, or what library or object do you need to load in order for
DB_NAME
to be recognized, and not be an uninitialized constant? Commented Sep 13, 2015 at 23:33 -
1The constant DB_NAME is not always defined - when the HyperDB plugin is in use (replaces WPDB class to support multiple databases), then having a single DB_NAME constant wouldn't make sense– abhij89Commented Apr 8, 2021 at 11:32