Title: WordPress have a DB query api
Last modified: August 19, 2016

---

# WordPress have a DB query api

 *  Resolved [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/)
 * I’m writing plugin for wordpress that requires DB access. Is there a way of accessing
   the config.php file so that I can load the username, password, and DB in use?
   
   Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695740)
 * Just adding `require('./wp-blog-header.php');` to your third-party code will 
   load all of WordPress, without displaying anything. Then you can access the WordPress
   database through the $wpdb object.
 * If you’re writing a WordPress plugin, then you already have all that information.
   No need to load anything. Just use the global $wpdb to access the database directly.
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695743)
 * Thanks for the help. I really appreciate it.
    This may be a foolish question 
   but can anyone show me how to extract the username, password and DB name out 
   of the wpdb? My php skills aren’t quite up to snuff.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695757)
 * Why do you need the username, password, and DB name?
 * The WPDB object is already connected to the database. You can do SQL right through
   it. You don’t need to connect to it yourself.
 * Example:
    `$allposts = $wpdb->get_row("SELECT * FROM $wpdb->posts");`
 * Read here:
    [http://codex.wordpress.org/Function_Reference/wpdb_Class](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 * If you *really* want the username and password and such for some strange reason,
   then just reference them. They’re already defined.
 *     ```
       echo DB_NAME;
       echo DB_USER;
       ```
   
 * Just like that.
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695761)
 * Ok thanks I’ll try those functions
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695858)
 * The DB request isn;t working right. When I try to load the values for the DB 
   manually in a constructor it works fine but if I don’t plug in those values it
   seems to get them wrong. WordPress software does create rows in the DB itself
   in other areas so I know it must have access to the DB login values but I’m not
   getting them. Can anyone tell me what I’m doing wrong?
 *     ```
       function addZipcodeToDB($avatar, $user)
       {
       global $wpdb;
   
       //This was an experiment to see if that let us
       //connect correctly. It did.
       //$wpdb = new wpdb("root", "root", "wordpress", "localhost");
   
       $wpdb->show_errors();
       $query="UPDATE bb_users SET avatar =\"".$avatar."\" WHERE user_login= \"".$user."\"";
   
       $wpdb->query($query);
       }
       ```
   
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695874)
 * You don’t need to construct the database connection. It’s automatically constructed
   as a global when WordPress is loaded. If it’s not there, you’re not loading WordPress.
 * In fact, you can’t even declare the wpdb class without creating that global. 
   Look at the very bottom of wp-db.php, that’s where it instantiates the class 
   into $wpdb.
 * In other words, in a WordPress plugin, this would work:
 *     ```
       function addZipcodeToDB($avatar, $user)
       {
       global $wpdb;
       $wpdb->show_errors();
       $query="UPDATE bb_users SET avatar =\"".$avatar."\" WHERE user_login= \"".$user."\"";
       $wpdb->query($query);
       }
       ```
   
 * I’ve used code similar to that in other plugins and it works fine. Just make 
   sure the correct information is set in the wp-config.php file.
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695876)
 * Actually part of the problem, which I sadly forgot to mention, was I couldn’t
   import wp-config. I’m using bbpress and apparently there is a duplicate declaration
   of _http_build_query() in bbpress’s wp-functions.php. It is wrapped in an if 
   statement claiming the function will only be created if it does not already exist
   but it doesn’t seem to be working. Any idea why?
    Thanks a lot
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695878)
 * What do you mean that you can’t import wp-config? You don’t import wp-config,
   you import wp-blog-header.php to load WordPress.
 * And you can’t have it halfway. Either you load all of WordPress or none of it.
   It’s not modular. If you’re have bbPress troubles, you should try the bbPress
   support forums. But, in general, you need to load WordPress *first*. Then you
   won’t get that error.
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695879)
 * I’m sorry I am new at this. I read in the DB api tutorial to import
 * include_once(‘../wp-config.php’);
    include_once(‘../wp-includes/wp-db.php’);
 * which I did thinking that was the correct way to get access to the wordpress 
   DB functions. When that failed I tried using
 * include_once(‘config.php’);
    include_once(‘../wp-includes/wp-db.php’);
 * i.e. importing wordpress DB functions and the config for bbpress (which do have
   the same DB seettings) and that gave me no exceptions except that it still won’t
   update the DB.
 * Am I importing the wrong files? I got the file names from the [http://codex.wordpress.org/Function_Reference/wpdb_Class](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 *  Thread Starter [darkling235](https://wordpress.org/support/users/darkling235/)
 * (@darkling235)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695880)
 * Ok I managed to use bbforums DB connection to do it. Thanks to everyone who helped
   me work this out

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘WordPress have a DB query api’ is closed to new replies.

 * 10 replies
 * 2 participants
 * Last reply from: [darkling235](https://wordpress.org/support/users/darkling235/)
 * Last activity: [18 years, 4 months ago](https://wordpress.org/support/topic/wordpress-have-a-db-query-api/#post-695880)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
