[wp-hackers] What is the correct way to open and close another database from a plugin?

Scott Merrill skippy at skippy.net
Wed Jul 14 01:13:59 UTC 2004


David House wrote:
> Quoting Ian Barton <lists at manor-farm.org>:
> 
>>wp-db.php has a class to open and close a database, but this seems to be 
>>hard wired to the WordPress db.
> 
> 
> Try this:
> 
> $mynewdbconn = new wpdb($username, $password, $dbname, $host);
> 
> wpdb is just a class: you can create as many instances of it as you want. The
> variable $wpdb happens to be one created by default, and it is this variable,
> not the class, that is 'hard-wired' into the WP database.

That works for wp-db functions, but unfortunately all the other WP 
functions are hard-wired into the global $wpdb object.

For example, I'd like comment-enable some non-blog data without 
re-inventing the commenting wheel.  I'd like to use the WP comment 
functions; but in order to do that, I need an alternate config file 
(say, my-config.php), which specifies my table prefix.  Unfortunately 
the config file calls wp-settings.php, which in turn calls vars.php, 
which requires the entirety of the WP database layout.  I don't want or 
need a completely duplicated WP database structure just to support 
comments on some non-blog data -- I just need the comment bits!

Consider this a feature request for more modular -- or override-able -- 
core functions.



More information about the hackers mailing list