Index: wp-includes/wp-db.php
===================================================================
--- wp-includes/wp-db.php	(revision 6295)
+++ wp-includes/wp-db.php	(working copy)
@@ -20,6 +20,7 @@
 	var $last_query;
 	var $col_info;
 	var $queries;
+	var $prefix = '';
 
 	// Our tables
 	var $posts;
@@ -29,16 +30,13 @@
 	var $comments;
 	var $links;
 	var $options;
-	var $optiontypes;
-	var $optionvalues;
-	var $optiongroups;
-	var $optiongroup_options;
 	var $postmeta;
 	var $usermeta;
 	var $terms;
 	var $term_taxonomy;
 	var $term_relationships;
-
+	var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
+			'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
 	var $charset;
 	var $collate;
 
@@ -86,6 +84,26 @@
 		return true;
 	}
 
+	function set_prefix($prefix) {
+
+		if ( preg_match('|[^a-z0-9_]|i', $prefix) )
+			return new WP_Error('invalid_db_prefix', 'Invalid database prefix'); // No gettext here
+
+		$old_prefix = $this->prefix;
+		$this->prefix = $prefix;
+
+		foreach ( $this->tables as $table )
+			$this->$table = $this->prefix . $table;
+
+		if ( defined('CUSTOM_USER_TABLE') )
+			$this->users = CUSTOM_USER_TABLE;
+
+		if ( defined('CUSTOM_USER_META_TABLE') )
+			$this->usermeta = CUSTOM_USER_META_TABLE;
+
+		return $old_prefix;
+	}
+
 	/**
 	 * Selects a database using the current class's $this->dbh
 	 * @param string $db name
Index: wp-settings.php
===================================================================
--- wp-settings.php	(revision 6295)
+++ wp-settings.php	(working copy)
@@ -118,34 +118,14 @@
 
 require (ABSPATH . WPINC . '/compat.php');
 require (ABSPATH . WPINC . '/functions.php');
+require (ABSPATH . WPINC . '/classes.php');
 
 require_wp_db();
-// $table_prefix is deprecated as of 2.1
-$wpdb->prefix = $table_prefix;
+$prefix = $wpdb->set_prefix($table_prefix);
 
-if ( preg_match('|[^a-z0-9_]|i', $wpdb->prefix) && !file_exists(ABSPATH . 'wp-content/db.php') )
+if ( is_wp_error($prefix) )
 	wp_die("<strong>ERROR</strong>: <code>$table_prefix</code> in <code>wp-config.php</code> can only contain numbers, letters, and underscores.");
 
-// Table names
-$wpdb->posts          = $wpdb->prefix . 'posts';
-$wpdb->users          = $wpdb->prefix . 'users';
-$wpdb->categories     = $wpdb->prefix . 'categories';
-$wpdb->post2cat       = $wpdb->prefix . 'post2cat';
-$wpdb->comments       = $wpdb->prefix . 'comments';
-$wpdb->link2cat       = $wpdb->prefix . 'link2cat';
-$wpdb->links          = $wpdb->prefix . 'links';
-$wpdb->options        = $wpdb->prefix . 'options';
-$wpdb->postmeta       = $wpdb->prefix . 'postmeta';
-$wpdb->usermeta       = $wpdb->prefix . 'usermeta';
-$wpdb->terms          = $wpdb->prefix . 'terms';
-$wpdb->term_taxonomy  = $wpdb->prefix . 'term_taxonomy';
-$wpdb->term_relationships = $wpdb->prefix . 'term_relationships';
-
-if ( defined('CUSTOM_USER_TABLE') )
-	$wpdb->users = CUSTOM_USER_TABLE;
-if ( defined('CUSTOM_USER_META_TABLE') )
-	$wpdb->usermeta = CUSTOM_USER_META_TABLE;
-
 if ( file_exists(ABSPATH . 'wp-content/object-cache.php') )
 	require_once (ABSPATH . 'wp-content/object-cache.php');
 else
@@ -153,7 +133,6 @@
 
 wp_cache_init();
 
-require (ABSPATH . WPINC . '/classes.php');
 require (ABSPATH . WPINC . '/plugin.php');
 require (ABSPATH . WPINC . '/default-filters.php');
 include_once(ABSPATH . WPINC . '/streams.php');

