Index: wordpress/wp-includes/wp-db-driver-mysqli.php
===================================================================
--- wordpress/wp-includes/wp-db-driver-mysqli.php	(revision 0)
+++ wordpress/wp-includes/wp-db-driver-mysqli.php	(working copy)
@@ -0,0 +1,148 @@
+<?php
+
+/**
+ * WordPress Database Access Abstraction Object
+ *
+ * It is possible to replace this class with your own
+ * by setting the $wpdb global variable in wp-content/db.php
+ * file to your class. The wpdb class will still be included,
+ * so you can extend it or simply use your own.
+ *
+ * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
+ *
+ * @package WordPress
+ * @subpackage Database
+ * @since 0.71
+ */
+class wpdb_driver_mysqli implements wpdb_driver {
+	
+	/**
+	 * Database link
+	 * @var mysqli
+	 */
+	private $dbh = null;
+	
+	/**
+	 * Result set
+	 * @var mysqli_stmt|mysqli_result
+	 */
+	private $result = null;
+	
+	/**
+	 * Cached column info
+	 * @var array|null
+	 */
+	private $col_info = null;
+	
+	/**
+	 * Escape with mysql_real_escape_string()
+	 * @param  string $string
+	 * @return string
+	 */
+	public function escape( $string ) {
+		return $this->dbh->escape_string( $string );
+	}
+
+	/**
+	 * Get the latest error message from the DB driver
+	 * @return string
+	 */
+	public function get_error_message() {
+		return $this->dbh->error;
+	}
+
+	/**
+	 * Free memory associated with the resultset
+	 * @return void
+	 */
+	public function flush() {
+		if ( $this->result instanceof mysqli_stmt ) {
+			$this->result->free_result();
+		}
+		$this->result = null;
+		$this->col_info = null;
+	}
+
+	/**
+	 * Connect to database
+	 * @return bool
+	 */
+	public function connect( $host, $user, $pass, $port = 3306 ) {		
+		$this->dbh = new mysqli( $host, $user, $pass, '', $port );
+		return ( !mysqli_connect_error() );
+	}
+
+	/**
+	 * Select database
+	 * @return void
+	 */
+	public function select( $db ) {
+		$this->dbh->select_db( $db );
+	}		
+	
+	/**
+	 * Perform a MySQL database query, using current database connection.
+	 * @param string $query Database query
+	 * @return int|false Number of rows affected/selected or false on error
+	 */
+	public function query( $query ) {
+		$this->result = $this->dbh->query( $query );
+		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
+			$return_val = $this->result;
+		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
+			$return_val = $this->affected_rows();
+		} elseif ( preg_match( '/^\s*select\s/i', $query ) ) {
+			return $this->result->num_rows;
+		}
+		return true;
+	}
+
+	/**
+	 * Get number of rows affected
+	 * @return int
+	 */
+	public function affected_rows() {
+		return $this->dbh->affected_rows;
+	}
+	
+	/**
+	 * Get last insert id
+	 * @return int
+	 */
+	public function insert_id() {
+		return $this->dbh->insert_id;
+	}
+
+	/**
+	 * Get results
+	 * @return array
+	 */
+	public function get_results() {
+		$ret = array();
+		while ( $row = $this->result->fetch_object() ) {
+			$ret[] = $row;
+		}
+		return $ret;
+	}
+
+	/**
+	 * Load the column metadata from the last query.
+	 * @return array
+	 */
+	public function load_col_info() {
+		if ( $this->col_info )
+			return $this->col_info;
+		for ( $i = 0; $i < $this->result->field_count ; $i++ ) {
+			$this->col_info[ $i ] = $this->result->fetch_field_direct( $i );
+		}
+		return $this->col_info;
+	}
+
+	/**
+	 * The database version number.
+	 * @return false|string false on failure, version number on success
+	 */
+	public function db_version() {
+		return preg_replace( '/[^0-9.].*/', '', $this->dbh->server_version );
+	}
+}
\ No newline at end of file
Index: wordpress/wp-includes/wp-db-driver.interface.php
===================================================================
--- wordpress/wp-includes/wp-db-driver.interface.php	(revision 0)
+++ wordpress/wp-includes/wp-db-driver.interface.php	(working copy)
@@ -0,0 +1,15 @@
+<?php
+
+interface wpdb_driver {
+	public function escape( $string );
+	public function get_error_message();
+	public function flush();
+	public function connect( $host, $user, $pass, $port = 3306 );
+	public function select( $name );
+	public function query( $query );
+	public function load_col_info();
+	public function db_version();
+	public function affected_rows();
+	public function insert_id();
+	public function get_results();
+}
Index: wordpress/wp-includes/wp-db-driver-mysql.php
===================================================================
--- wordpress/wp-includes/wp-db-driver-mysql.php	(revision 0)
+++ wordpress/wp-includes/wp-db-driver-mysql.php	(working copy)
@@ -0,0 +1,160 @@
+<?php
+
+/**
+ * WordPress Database Access Abstraction Object
+ *
+ * It is possible to replace this class with your own
+ * by setting the $wpdb global variable in wp-content/db.php
+ * file to your class. The wpdb class will still be included,
+ * so you can extend it or simply use your own.
+ *
+ * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
+ *
+ * @package WordPress
+ * @subpackage Database
+ * @since 0.71
+ */
+class wpdb_driver_mysql implements wpdb_driver {
+	
+	/**
+	 * Database link
+	 * @var resource
+	 */
+	private $dbh = null;
+	
+	/**
+	 * Result set
+	 * @var resource
+	 */
+	private $result = null;
+	
+	/**
+	 * Cached column info
+	 * @var array|null
+	 */
+	private $col_info = null;
+	
+	/**
+	 * Escape with mysql_real_escape_string()
+	 * @param  string $string
+	 * @return string
+	 */
+	public function escape( $string ) {
+		return mysql_real_escape_string( $string, $this->dbh );
+	}
+
+	/**
+	 * Get the latest error message from the DB driver
+	 * @return string
+	 */
+	public function get_error_message() {
+		return mysql_error( $this->dbh );
+	}
+
+	/**
+	 * Free memory associated with the resultset
+	 * @return void
+	 */
+	public function flush() {
+		if ( is_resource( $this->result ) ) {
+			mysql_free_result( $this->result );
+		}
+		$this->result = null;
+		$this->col_info = null;
+	}
+
+	/**
+	 * Connect to database
+	 * @return bool
+	 */
+	public function connect( $host, $user, $pass, $port = 3306 ) {
+		
+		$new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
+		$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
+
+		if ( WP_DEBUG ) {
+			$this->dbh =  mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags );
+		} else {
+			$this->dbh = @mysql_connect( "$host:$port", $user, $pass, $new_link, $client_flags );
+		}
+		return ( false !== $this->dbh );
+	}
+
+	/**
+	 * Select database
+	 * @return void
+	 */
+	public function select( $db ) {
+		if ( WP_DEBUG ) {
+			 mysql_select_db( $db, $this->dbh );
+		} else {
+			@mysql_select_db( $db, $this->dbh );
+		}
+	}		
+	
+	/**
+	 * Perform a MySQL database query, using current database connection.
+	 * @param string $query Database query
+	 * @return int|false Number of rows affected/selected or false on error
+	 */
+	public function query( $query ) {
+		$this->result = @mysql_query( $query, $this->dbh );
+		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
+			$return_val = $this->result;
+		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
+			$return_val = $this->affected_rows();
+		} elseif ( preg_match( '/^\s*select\s/i', $query ) ) {
+			return is_resource( $this->result ) ? mysql_num_rows( $this->result ) : false ;
+		}
+		return true;
+	}
+
+	/**
+	 * Get number of rows affected
+	 * @return int
+	 */
+	public function affected_rows() {
+		return mysql_affected_rows( $this->dbh );
+	}
+	
+	/**
+	 * Get last insert id
+	 * @return int
+	 */
+	public function insert_id() {
+		return mysql_insert_id( $this->dbh );
+	}
+
+	/**
+	 * Get results
+	 * @return array
+	 */
+	public function get_results() {
+		$ret = array();
+		while ( $row = @mysql_fetch_object( $this->result ) ) {
+			$ret[] = $row;
+		}
+		return $ret;
+	}
+
+	/**
+	 * Load the column metadata from the last query.
+	 * @return array
+	 */
+	public function load_col_info() {
+		if ( $this->col_info )
+			return $this->col_info;
+		for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
+			$this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
+		}
+		return $this->col_info;
+	}
+
+	/**
+	 * The database version number.
+	 * @return false|string false on failure, version number on success
+	 */
+	public function db_version() {
+		return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
+	}
+}
\ No newline at end of file
Index: wordpress/wp-includes/wp-db-driver-pdo_mysql.php
===================================================================
--- wordpress/wp-includes/wp-db-driver-pdo_mysql.php	(revision 0)
+++ wordpress/wp-includes/wp-db-driver-pdo_mysql.php	(working copy)
@@ -0,0 +1,170 @@
+<?php
+
+/**
+ * WordPress Database Access Abstraction Object
+ *
+ * It is possible to replace this class with your own
+ * by setting the $wpdb global variable in wp-content/db.php
+ * file to your class. The wpdb class will still be included,
+ * so you can extend it or simply use your own.
+ *
+ * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
+ *
+ * @package WordPress
+ * @subpackage Database
+ * @since 0.71
+ */
+class wpdb_driver_pdo_mysql implements wpdb_driver {
+	
+	/**
+	 * Database link
+	 * @var PDO
+	 */
+	private $dbh = null;
+	
+	/**
+	 * Result set
+	 * @var PDOStatement
+	 */
+	private $result = null;
+	
+	/**
+	 * Cached column info
+	 * @var array|null
+	 */
+	private $col_info = null;
+	
+	/**
+	 * Array of fetched rows.
+	 * PDO doesn't have a "count rows" feature, so we have to fetch the rows
+	 * up front, and cache them here
+	 * @var array
+	 */
+	private $fetched_rows = array();
+
+	/**
+	 * Escape with mysql_real_escape_string()
+	 * @param  string $string
+	 * @return string
+	 */
+	public function escape( $string ) {
+		return substr( $this->dbh->quote( $string ), 1, -1 );
+	}
+
+	/**
+	 * Get the latest error message from the DB driver
+	 * @return string
+	 */
+	public function get_error_message() {
+		$error = $this->dbh->errorInfo();
+		return $error[2];
+	}
+
+	/**
+	 * Free memory associated with the resultset
+	 * @return void
+	 */
+	public function flush() {
+		if ( $this->result instanceof PDOStatement ) {
+			$this->result->closeCursor();
+		}
+		$this->result = null;
+		$this->col_info = null;
+		$this->fetched_rows = array();
+	}
+
+	/**
+	 * Connect to database
+	 * @return bool
+	 */
+	public function connect( $host, $user, $pass, $port = 3306 ) {
+		$dsn = sprintf( 'mysql:host=%1$s;port=%2$d', $host, $port );
+		try {
+			$this->dbh = new PDO( $dsn, $user, $pass );
+		} catch ( Exception $e ) {
+			return false;
+		}
+		return true;
+	}
+
+	/**
+	 * Select database
+	 * @return void
+	 */
+	public function select( $db ) {
+		$this->dbh->exec( sprintf( 'USE %s', $db ) );
+	}		
+	
+	/**
+	 * Perform a MySQL database query, using current database connection.
+	 * @param string $query Database query
+	 * @return int|false Number of rows affected/selected or false on error
+	 */
+	public function query( $query ) {
+		try {
+			$this->result = $this->dbh->query( $query );
+		} catch ( Exception $e ) {
+		}
+		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
+			$return_val = $this->result;
+		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
+			$return_val = $this->affected_rows();
+		} elseif ( preg_match( '/^\s*select\s/i', $query ) ) {
+			$this->get_results();
+			return count( $this->fetched_rows );
+		}
+		return true;
+	}
+
+	/**
+	 * Get number of rows affected
+	 * @return int
+	 */
+	public function affected_rows() {
+		return $this->result->rowCount();
+	}
+	
+	/**
+	 * Get last insert id
+	 * @return int
+	 */
+	public function insert_id() {
+		return $this->dbh->lastInsertId();
+	}
+
+	/**
+	 * Get results
+	 * @return array
+	 */
+	public function get_results() {
+		if ( !empty( $this->fetched_rows ) ) {
+			return $this->fetched_rows;
+		}
+		$this->fetched_rows = array();
+		while ( $row = $this->result->fetchObject() ) {
+			$this->fetched_rows[] = $row;
+		}
+		return $this->fetched_rows;
+	}
+
+	/**
+	 * Load the column metadata from the last query.
+	 * @return array
+	 */
+	public function load_col_info() {
+		if ( $this->col_info )
+			return $this->col_info;
+		for ( $i = 0; $i < $this->result->columnCount() ; $i++ ) {
+			$this->col_info[ $i ] = $this->result->fetchColumn( $i );
+		}
+		return $this->col_info;
+	}
+
+	/**
+	 * The database version number.
+	 * @return false|string false on failure, version number on success
+	 */
+	public function db_version() {
+		return preg_replace( '/[^0-9.].*/', '', $this->dbh->getAttribute( PDO::ATTR_SERVER_VERSION ) );
+	}
+}
\ No newline at end of file
Index: wordpress/wp-includes/wp-db.php
===================================================================
--- wordpress/wp-includes/wp-db.php	(revision 21612)
+++ wordpress/wp-includes/wp-db.php	(working copy)
@@ -487,11 +487,14 @@
 	protected $dbhost;
 
 	/**
-	 * Database Handle
+	 * Database driver object
 	 *
-	 * @since 0.71
+	 * Allow for simple, and backwards compatible, pluggable database drivers
+	 * like PDO_mysql and mysqli to power wpdb
+	 * 
 	 * @access protected
-	 * @var string
+	 * @since 3.5.0
+	 * @var wpdb_driver
 	 */
 	protected $dbh;
 
@@ -519,6 +522,50 @@
 	public $is_mysql = null;
 
 	/**
+	 * Pick the right adapter and return an instance
+	 * @static
+	 * @since 3.5.0
+	 * @param string $dbuser MySQL database user
+	 * @param string $dbpassword MySQL database password
+	 * @param string $dbname MySQL database name
+	 * @param string $dbhost MySQL database host
+	 * @return wpdb
+	 */
+	public function get_driver( $driver = null ) {
+		
+		// Auto-pick the driver
+		if ( null === $driver ) {
+			if ( extension_loaded( 'mysql' ) ) {
+				$driver = 'mysql';
+			} elseif ( extension_loaded( 'mysqli' ) ) {
+				$driver = 'mysqli';
+			} elseif ( extension_loaded( 'pdo_mysql' ) ) {
+				$driver = 'pdo_mysql';
+			} else {
+				wp_load_translations_early();
+				$this->bail( sprintf( __( "
+					<h1>No database drivers found</h1>.
+					<p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p>
+					<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>
+				"), 'db_connect_fail' ) );
+			}
+		} elseif ( !in_array( $driver, array( 'mysql', 'mysqli', 'pdo_mysql' ) ) ) {
+			wp_load_translations_early();
+			$this->bail( sprintf( __( "
+				<h1>No database drivers found</h1>.
+				<p>WordPress requires the mysql, mysqli, or pdo_mysql extension to talk to your database.</p>
+				<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>
+			"), 'db_connect_fail' ) );
+		}
+
+		// Get the new driver
+		require_once(dirname(dirname(__FILE__)) . '/wp-includes/wp-db-driver.interface.php');
+		require_once(dirname(dirname(__FILE__)) . '/wp-includes/wp-db-driver-' . $driver . '.php');
+		$class = 'wpdb_driver_' . $driver;
+		$this->dbh = new $class();
+	}
+
+	/**
 	 * Connects to the database server and selects a database
 	 *
 	 * PHP5 style constructor for compatibility with PHP5. Does
@@ -534,6 +581,9 @@
 	 * @param string $dbhost MySQL database host
 	 */
 	function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
+		
+		$this->get_driver();
+
 		register_shutdown_function( array( &$this, '__destruct' ) );
 
 		if ( WP_DEBUG )
@@ -646,15 +696,10 @@
 		if ( !isset($collate) )
 			$collate = $this->collate;
 		if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
-			if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
-				mysql_set_charset( $charset, $dbh );
-				$this->real_escape = true;
-			} else {
-				$query = $this->prepare( 'SET NAMES %s', $charset );
-				if ( ! empty( $collate ) )
-					$query .= $this->prepare( ' COLLATE %s', $collate );
-				mysql_query( $query, $dbh );
-			}
+			$query = $this->prepare( 'SET NAMES %s', $charset );
+			if ( ! empty( $collate ) )
+				$query .= $this->prepare( ' COLLATE %s', $collate );
+			$this->dbh->query( $query );
 		}
 	}
 
@@ -837,22 +882,7 @@
 	 * @return null Always null.
 	 */
 	function select( $db, $dbh = null ) {
-		if ( is_null($dbh) )
-			$dbh = $this->dbh;
-
-		if ( !@mysql_select_db( $db, $dbh ) ) {
-			$this->ready = false;
-			wp_load_translations_early();
-			$this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
-<p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
-<ul>
-<li>Are you sure it exists?</li>
-<li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
-<li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
-</ul>
-<p>If you don\'t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' );
-			return;
-		}
+		$this->dbh->select( $db );
 	}
 
 	/**
@@ -882,7 +912,7 @@
 	 */
 	function _real_escape( $string ) {
 		if ( $this->dbh && $this->real_escape )
-			return mysql_real_escape_string( $string, $this->dbh );
+			return $this->dbh->escape( $string );
 		else
 			return addslashes( $string );
 	}
@@ -1017,7 +1047,7 @@
 		global $EZSQL_ERROR;
 
 		if ( !$str )
-			$str = mysql_error( $this->dbh );
+			$str = $this->dbh->get_error_message();
 		$EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
 
 		if ( $this->suppress_errors )
@@ -1118,12 +1148,7 @@
 	 * @return void
 	 */
 	function flush() {
-		$this->last_result = array();
-		$this->col_info    = null;
-		$this->last_query  = null;
-
-		if ( is_resource( $this->result ) )
-			mysql_free_result( $this->result );
+		$this->dbh->flush();
 	}
 
 	/**
@@ -1135,16 +1160,12 @@
 
 		$this->is_mysql = true;
 
-		$new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
-		$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
-
-		if ( WP_DEBUG ) {
-			$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
-		} else {
-			$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
+		$host = $this->dbhost;
+		$port = 3306;
+		if ( false !== strpos( $this->dbhost, ':' ) ) {
+			list( $host, $port ) = explode( ':', $this->dbhost );
 		}
-
-		if ( !$this->dbh ) {
+		if ( !$this->dbh->connect( $host, $this->dbuser, $this->dbpassword, $port ) ) {
 			wp_load_translations_early();
 			$this->bail( sprintf( __( "
 <h1>Error establishing a database connection</h1>
@@ -1196,14 +1217,14 @@
 		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
 			$this->timer_start();
 
-		$this->result = @mysql_query( $query, $this->dbh );
+		$result = $this->dbh->query( $query );
 		$this->num_queries++;
 
 		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
 			$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
 
 		// If there is an error then take note of it..
-		if ( $this->last_error = mysql_error( $this->dbh ) ) {
+		if ( $this->last_error = $this->dbh->get_error_message() ) {
 			$this->print_error();
 			return false;
 		}
@@ -1211,26 +1232,18 @@
 		if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
 			$return_val = $this->result;
 		} elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
-			$this->rows_affected = mysql_affected_rows( $this->dbh );
+			$this->rows_affected = $this->dbh->affected_rows();
 			// Take note of the insert_id
 			if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
-				$this->insert_id = mysql_insert_id($this->dbh);
+				$this->insert_id = $this->dbh->insert_id();
 			}
 			// Return number of rows affected
 			$return_val = $this->rows_affected;
 		} else {
-			$num_rows = 0;
-			while ( $row = @mysql_fetch_object( $this->result ) ) {
-				$this->last_result[$num_rows] = $row;
-				$num_rows++;
-			}
-
-			// Log number of rows the query returned
-			// and return number of rows selected
-			$this->num_rows = $num_rows;
-			$return_val     = $num_rows;
+			$this->last_result = $this->dbh->get_results();
+			$return_val = $this->num_rows = count( $this->last_result );
 		}
-
+		
 		return $return_val;
 	}
 
@@ -1560,12 +1573,7 @@
 	 * @access protected
 	 */
 	protected function load_col_info() {
-		if ( $this->col_info )
-			return;
-
-		for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
-			$this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
-		}
+		$this->col_info = $this->dbh->load_col_info();
 	}
 
 	/**
@@ -1736,6 +1744,6 @@
 	 * @return false|string false on failure, version number on success
 	 */
 	function db_version() {
-		return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
+		return $this->dbh->db_version();
 	}
 }
