Use one DB connection on your custom wordpress install

Posted on May 11, 2008. Filed under: mysql+, PHP, WordPress | Tags: |

Like most people who use WordPress, I have hacked a few installs 6 ways from Sunday for some of my projects. While digging through my hacks I had noticed that the script was connecting to my database TWICE per client … once for my website it’s self, then once for WordPress’ needs. This seemed like an awful waste especially considering how much traffic the site can get … so why not share the connection resource [while effectively DOUBLING the amount of people who can connect to your blog — since 2 - 1 = 1]??

This is how I hacked WordPress to reuse an existing DB connection …

WordPress, meet my web site
I started with the root of the script, ./index.php, and added in my global include file [.gfl == global function library, which, among other things, connects to the DB for me]. This way no matter what WordPress does on the front end, it will also have all my web site’s functions as well.


<?php
require("../global.gfl");
/* Short and sweet */
define('WP_USE_THEMES', true);
require('./wp-blog-header.php');
?>

 

Now play nice!
After changing my mysql_connect resource to a global variable:


<?php
    global $dbResource;
    $dbResource = mysql_connect("host","user","pass");
    mysql_select_db("db");
?>

I did a quick search for the string "mysql_connect" within my WordPress directory I found it living in ./wp-includes/wp-db.php. I changed …


<?php
    function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
        # Code omitted

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

        # Code omitted
    }
?>

… to …


<?php
    function __construct($dbuser, $dbpassword, $dbname, $dbhost, true) {
        global $dbResource;

        if (is_resource($dbResource)) {
            # For the front-end ...
            $this->dbh = $dbResource;
        } else {
            # For wp-admin ...
            # Code omitted

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

            # Code omitted
        }
    }
?>

Simple enough!

Comments, critiques, suggestions, recommendations, changes, etc etc etc are always welcome!

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

3 Responses to “Use one DB connection on your custom wordpress install”

RSS Feed for Adventures in PHP / DHTML / CSS and MySQL Comments RSS Feed

That’s a cool hack indeed, but it gets overwritten each time you upgrade WP.
I think a better solution might be possible.

wp-includes/wp-db.php is included by function require_wp_db() in wp-includes/functions.php, which first checks if wp-content/db.php exists, and if not, includes wp-db.php

So, basically, I think you could simply replicate most of wp-db.php in your own wp-content/db.php (which would not get overwritten) and always be able to upgrade WP without minding about having to hack core files.

Hmmm … I had no idea :-) … makes a lot more sense for update purposes. My whole experience has been, “here’s WordPress, make it work in two days”, and I don’t know much about it’s internal workings.

I’ve had the 20 lbs. sledge approach where I search for functions and rewrite them to suit my needs instead of working with the code.

Thanks for the tip :-)

هيئة المهندسين التجمعيين – corps des ingenieurs du parti du RNI

corps des ingenieurs du parti du RNI


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: