WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 7 years ago

#2233 closed enhancement (invalid)

wpdb constructor should create a new connection

Reported by: thomasq Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.0
Component: Administration Keywords:
Focuses: Cc:

Description

The constructor for MySQL connections should create a new transport connection each time
it is called. Otherwise, an import plugin that wants to connect on a database that resides
on the same server as the Wordpress database may incorrectly mess up the Wordpress $wpdb.

Attachments (2)

d (651 bytes) - added by thomasq 8 years ago.
add_link_identifiers.patch (897 bytes) - added by abhay 8 years ago.

Download all attachments as: .zip

Change History (11)

thomasq8 years ago

  • Attachment d added

comment:1 davidhouse8 years ago

  • Keywords bg|dev-feedback added
  • Milestone set to 2.1
  • Severity changed from normal to enhancement

Would this break anything? I can't see WordPress creating another instance of $wpdb, so it should be harmless.

comment:2 thomasq8 years ago

I do not foresee any breakage; on my setup, Wordpress seems to be happy with the change.

comment:3 masquerade8 years ago

A better solution would be using $this->dbh as part of all the mysql calls in the wpdb class instead, so that anyone else who uses mysql calls can use their constructor, as you should, for their own code. This would also allow multiple instances of the wpdb class.

abhay8 years ago

comment:4 abhay8 years ago

I would agree with masquerade on that one. If we want to allow for multiple connections on the database, it makes more sense to include the link identifiers in all calls to the database. This allows for multiple unique instances of the database connection to be available if necessary. I've added a patch that does this. There are only two instances where it matters.

comment:5 Libertus8 years ago

masquerade, abhay,

The original patch from thomasq is correct. wpdb constructor must force a new database connection, otherwise imports from different databases on the same host with the same username and password as the main WP database will fail.

The problem is not where $this->dbh is used. The problem is that $this->dbh on two supposedly independent wpdb objects are references to the same underlying database connection object, making wpdb::select() change the active database for all of them, including the global $wpdb, which side-effect causes the failure.

comment:6 masquerade8 years ago

While partially correct, creating a new object will not do exactly what you think. Both instances of wpdb will use the same database link either way, because $this->dbh is not used in the calls to mysql_*, so either way, both are needed, thomasq's patch is insufficient.

comment:7 matt7 years ago

  • Milestone changed from 2.1 to 2.2

comment:8 foolswisdom7 years ago

  • Keywords dev-feedback added; bg|dev-feedback removed
  • Milestone changed from 2.2 to 2.4

comment:9 rob1n7 years ago

  • Keywords dev-feedback removed
  • Milestone 2.4 deleted
  • Resolution set to invalid
  • Status changed from new to closed

wpdb now uses $this->dbh in all its mysql_* calls.

Note: See TracTickets for help on using tickets.