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

David House dave at xmouse.ithium.net
Mon Jul 12 15:57:42 UTC 2004


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.

Hope that helps.

> I want to be able to open and close another database from within my 
> plugin. If I use my standard boiler plate code:
> 
> // Connecting the DB
>      $Connection = mysql_connect(localhost, my_username, my_password) OR 
> die("Cannot Connect: mysql_error()");
>      mysql_select_db($DB_name, $Connection) OR die("DB SELECTION FAILED: 
> mysql_error()");
> 
>      // Select the data
>      $Query = "SELECT * FROM Quotes ORDER BY rand() LIMIT 1";
> 	$Result = mysql_query($Query);
> 	$Row = mysql_fetch_row($Result);
> 
>      // Close DB connection
>      mysql_close($Connection) OR die("CLOSE FAILED: mysql_error()");
> 
> I seem to end up closing the WordPress database, rather than the 
> connection I have created.
> 
> wp-db.php has a class to open and close a database, but this seems to be 
> hard wired to the WordPress db.
> 
> Ian.
> 
> _______________________________________________
> hackers mailing list
> hackers at wordpress.org
> http://wordpress.org/mailman/listinfo/hackers_wordpress.org
> 




----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the hackers mailing list