Archive for the 'PHP' Category

Upgrading WordPress using mysqli

Thursday, May 10th, 2007

Out-of-the-box WordPress assumes it’s going to be talking to a MySQL database using PHP’s original mysql extension instead of the newer mysqli extension (true up to WordPress 2.1.3 at least). If for some reason you need to run using mysqli, there are simple instructions on the WordPress support site: MySQLi for WordPress 2.1.x.

Switching to MySQLi was easy and worked as described, except for one thing: I couldn’t upgrade WordPress. After a bit of sleuthing I found there’s a MySQL version check buried in the upgrade process. For the current version (WordPress 2.1.3) it’s checking that MySQL is version 4.0.0 or higher. According to the PHP documentation for mysqli, it needs to run on MySQL 4.1 or above - so the version check is redundant.

Short-term fix: follow the first few steps of the WordPress upgrade process but before running the upgrade program edit wp-admin/upgrade-functions.php and gut this function:

function wp_check_mysql_version() {
    // (version-check removed)
}

You should be able to run the upgrade program fine now.

A handy PHP debug function

Thursday, May 3rd, 2007

Like most developers, I often find it useful to spit out a few messages in the middle of a PHP script - especially when I’m debugging some problems.

Sadly, after only a few months our pristine code-base has become spotted with this, over and over again:

if (DEBUG) {
    echo "New user object: "; var_dump($user); echo "\n”;
}

After discovering var_export I felt like I’d improved things by switching to:

if (DEBUG) {
    printf("New user object: %s\n”, var_export($user, true));
}

I still didn’t like the fact that if (DEBUG) was appearing all over the place. So, after stumbling across call_user_func_array I’ve implemented by own debug function:

function debug()
{
    if (DEBUG)
    {
	$args = func_get_args();
	$args[0] = "<pre>" . $args[0] . "</pre>\n";
	for ($i = 1, $l = count($args); $i < $l; $i++) {
	    $args[$i] = htmlspecialchars(var_export($args[$i], true));
	}
	call_user_func_array('printf', $args);
    }
}

Which means I can remove all the if (DEBUG) statements, leaving only:

debug("New user object: %s", $user);

I think that’s much cleaner.