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.
-
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.
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.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.ww.wp.xz.cn/Function_Reference/wpdb_ClassIf 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.
Ok thanks I’ll try those functions
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); }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.
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 lotWhat 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.
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.ww.wp.xz.cn/Function_Reference/wpdb_Class
Ok I managed to use bbforums DB connection to do it. Thanks to everyone who helped me work this out
The topic ‘WordPress have a DB query api’ is closed to new replies.