Make WordPress Core

Opened 18 years ago

Closed 16 years ago

#2722 closed enhancement (fixed)

Using multiple wpdb instances

Reported by: philnate's profile Philnate Owned by: nazgul's profile Nazgul
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.0.2
Component: General Keywords: wpdb class has-patch
Focuses: Cc:


For my plugin I need to allow the user to use different databases, hosts, users and passwords therefor I created my own wpdb class instance:

         $phpbbdb= new wpdb($pBBuser, $pBBpassword, $pBBdb, $pBBhost);

But if now something after this line access the database through the wpdb instance is using the phpbbdb resource id and therefor the Query isn't successful as the database don't hold these wordpress tables.
To remove this problem I need to use this line at the end of my code:

$phpbbdb= new wpdb(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST); 

As I found out, this problem can easily be resolved by changing this line, within the wp-db.php file:

		$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);

into this one:

		$this->dbh = @mysql_pconnect($dbhost, $dbuser, $dbpassword);

I'm not sure if this change would create other problems as I'm not too deep into PHP+MySQL, but while working with the persistance connection, I didn't encounter some Problems.


Attachments (1)

2722.diff (1.5 KB) - added by Nazgul 16 years ago.

Download all attachments as: .zip

Change History (9)

#1 @philnate
18 years ago

  • Milestone set to 2.0.3

Maybe atleast the option for a persistence connection should be added, as a new optional parameter. Which uses in default a non persistence connection. So no changes would be needed in templates and other components.

#2 @masquerade
18 years ago

You're barking up the wrong tree. What you should be looking at is passing $this->dbh to every query, and changing mysql_connect's fourth parameter (bool new_link) to true, so that you get a new connection for each instance of the class. Persistant connections often have serious adverse side-effects, and your milage will vary based on server specifications, configuration, and traffic. We unfortunately do not have the luxury of assuming everyone has a server configured properly enough to not die under the load that persistant connections potentially creates when it comes under high traffic.

#3 @philnate
18 years ago

But to use this parameter would cause that Wordpress needs atleast MySQL 4.2 .
Am I right if this change will mostly not be added as this would set the requirements too high?
So I need to provide the "fix" within the widgets?

#4 @foolswisdom
18 years ago

  • Milestone changed from 2.0.3 to 2.2

#5 @foolswisdom
17 years ago

  • Milestone changed from 2.2 to 2.4

#6 @Nazgul
17 years ago

  • Owner changed from anonymous to Nazgul
  • Status changed from new to assigned

16 years ago

#7 @Nazgul
16 years ago

  • Keywords has-patch added
  • Summary changed from long winded way to use two wpdb instances to Using multiple wpdb instances
  • Type changed from defect to enhancement

#8 @ryan
16 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [6701]) Allow multiple db instances. Props Nazgul. fixes #2722

Note: See TracTickets for help on using tickets.