WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 6 years ago

#2722 closed enhancement (fixed)

Using multiple wpdb instances

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

Description

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.

Thanks

Attachments (1)

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

Download all attachments as: .zip

Change History (9)

comment:1 philnate8 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.

comment:2 masquerade8 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.

comment:3 philnate8 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?

comment:4 foolswisdom8 years ago

  • Milestone changed from 2.0.3 to 2.2

comment:5 foolswisdom7 years ago

  • Milestone changed from 2.2 to 2.4

comment:6 Nazgul7 years ago

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

Nazgul6 years ago

comment:7 Nazgul6 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

comment:8 ryan6 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.