--- wp-db.orig	2008-06-02 17:18:03.000000000 +0100
+++ wp-db.php	2008-06-03 09:17:53.000000000 +0100
@@ -44,6 +44,12 @@
 	var $charset;
 	var $collate;
 
+	// Our Database Connection Details
+	var $dbuser = '';
+	var $dbpassword = '';
+	var $dbname = '';
+	var $dbhost = '';
+
 	/**
 	 * Connects to the database server and selects a database
 	 * @param string $dbuser
@@ -67,7 +73,61 @@
 		if ( defined('DB_COLLATE') )
 			$this->collate = DB_COLLATE;
 
-		$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
+		$this->dbuser = $dbuser;
+		$this->dbpassword = $dbpassword;
+		$this->dbname = $dbname;
+		$this->dbhost = $dbhost;
+		
+		if (defined('DB_SLAVE_ENABLED'))
+		{
+			if (defined('DB_SLAVE_USERNAME') &&
+				defined('DB_SLAVE_PASSWORD') &&
+				defined('DB_SLAVE_HOSTNAME')
+			   )
+			{
+				$this->slave_username = DB_SLAVE_USERNAME;
+				$this->slave_password = DB_SLAVE_PASSWORD;
+				$this->slave_hostname = DB_SLAVE_HOSTNAME;
+			}
+		}
+		
+		$this->ready = true;
+	}
+
+	function __destruct() {
+		return true;
+	}
+
+	function create_database_connection($readOnly = false)
+	{
+		if ($this->dbh && $this->read_only == $readOnly)
+			return true;
+		
+		if (!$this->dbhost || !$this->dbuser || !$this->dbpassword || !$this->dbname)
+		{
+			$this->bail("
+<h1>Error establishing a database connection</h1>
+<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
+<ul>
+	<li>Are you sure you have the correct username and password?</li>
+	<li>Are you sure that you have typed the correct hostname?</li>
+	<li>Are you sure that the database server is running?</li>
+</ul>
+<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
+");
+		}
+		
+		if (defined('DB_SLAVE_ENABLED') && $readOnly)
+		{
+			$this->dbh = @mysql_connect($this->slave_hostname, $this->slave_username, $this->slave_password, true);
+			$this->read_only = true;
+		}
+		else
+		{
+			$this->dbh = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, true);
+			$this->read_only = false;
+		}
+		
 		if (!$this->dbh) {
 			$this->bail("
 <h1>Error establishing a database connection</h1>
@@ -87,11 +147,8 @@
 		if ( !empty($this->charset) && version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') )
  			$this->query("SET NAMES '$this->charset'");
 
-		$this->select($dbname);
-	}
+		$this->select($this->dbname);
 
-	function __destruct() {
-		return true;
 	}
 
 	function set_prefix($prefix) {
@@ -272,6 +329,8 @@
 		if (SAVEQUERIES)
 			$this->timer_start();
 
+		$readOnly = $this->isSelectquery($query);
+		$this->create_database_connection($readOnly);
 		$this->result = @mysql_query($query, $this->dbh);
 		++$this->num_queries;
 
@@ -527,6 +586,7 @@
 	{
 		global $wp_version;
 		// Make sure the server has MySQL 4.0
+		$this->create_database_connection(true);
 		$mysql_version = preg_replace('|[^0-9\.]|', '', @mysql_get_server_info($this->dbh));
 		if ( version_compare($mysql_version, '4.0.0', '<') )
 			return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version));
@@ -538,6 +598,7 @@
 	 */
 	function supports_collation()
 	{
+		$this->create_database_connection(true);
 		return ( version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') );
 	}
 
@@ -569,6 +630,14 @@
 		return $caller;
 	}
 
+	function isSelectquery($query) {
+		$query = ltrim($query);
+		if (preg_match('/^SELECT/iS', $query))
+		{
+			return true;
+		}
+		return false;
+	}
 }
 
 if ( ! isset($wpdb) )
