Wordpress mysqli 2.9 Database Class Patch

wordpress-logoWordpress does not officialy support the mysqli drivers.  I needed my Wordpress installation to use the mysqli class instead of the mysql ones. I’m sure there will be someone else out there with the same needs I decided to place the modified database class and the patch file online. Use whichever you prefer.

Patch contents

Index: wp-includes/wp-db.php
===================================================================
--- wp-includes/wp-db.php	(revision 19)
+++ wp-includes/wp-db.php	(working copy)
@@ -367,9 +367,12 @@
 			$this->collate = DB_COLLATE;

 		$this->dbuser = $dbuser;
+
+		// $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
+		$this->dbh = new mysqli($dbhost, $dbuser, $dbpassword, $dbname); 

-		$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
-		if (!$this->dbh) {
+		// if (!$this->dbh) {
+		if ($this->dbh->connect_error || mysqli_connect_error()) { // remember $mysqli->connect_error is broken in PHP < 5.2.9
 			$this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
 <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>%s</code>. This could mean your host's database server is down.</p>
@@ -386,18 +389,21 @@
 		$this->ready = true;

 		if ( !empty($this->charset) ) {
+			$this->dbh->set_charset($this->charset);
+			$this->real_escape = true;
+			/*
 			if ( function_exists('mysql_set_charset') ) {
 				mysql_set_charset($this->charset, $this->dbh);
-				$this->real_escape = true;
 			} else {
 				$collation_query = "SET NAMES '{$this->charset}'";
 				if ( !empty($this->collate) )
 					$collation_query .= " COLLATE '{$this->collate}'";
 				$this->query($collation_query);
-			}
+			}*/
 		}

-		$this->select($dbname);
+		// selection of database with mysql happens when connecting
+		// $this->select($dbname);
 	}

 	/**
@@ -454,6 +460,9 @@
 	 * @return null Always null.
 	 */
 	function select($db) {
+
+		return true; // database selection happens in constructor when usng mysqli
+
 		if (!@mysql_select_db($db, $this->dbh)) {
 			$this->ready = false;
 			$this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
@@ -475,7 +484,7 @@

 	function _real_escape($string) {
 		if ( $this->dbh && $this->real_escape )
-			return mysql_real_escape_string( $string, $this->dbh );
+			return $this->dbh->real_escape_string( $string );
 		else
 			return addslashes( $string );
 	}
@@ -579,7 +588,7 @@
 	function print_error($str = '') {
 		global $EZSQL_ERROR;

-		if (!$str) $str = mysql_error($this->dbh);
+		if (!$str) $str = $this->dbh->error;
 		$EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);

 		if ( $this->suppress_errors )
@@ -703,39 +712,40 @@
 		if ( defined('SAVEQUERIES') && SAVEQUERIES )
 			$this->timer_start();

-		$this->result = @mysql_query($query, $this->dbh);
+		$this->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->error) {
 			$this->print_error();
 			return false;
 		}

 		if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
-			$this->rows_affected = mysql_affected_rows($this->dbh);
+			$this->rows_affected = $this->db->affected_rows;
 			// Take note of the insert_id
 			if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
-				$this->insert_id = mysql_insert_id($this->dbh);
+				$this->insert_id = $this->db->insert_id;
 			}
 			// Return number of rows affected
 			$return_val = $this->rows_affected;
 		} else {
 			$i = 0;
-			while ($i < @mysql_num_fields($this->result)) {
-				$this->col_info[$i] = @mysql_fetch_field($this->result);
+			while ($i < $this->result->field_count) {
+				$this->col_info[$i] = @$this->result->fetch_field();
 				$i++;
 			}
 			$num_rows = 0;
-			while ( $row = @mysql_fetch_object($this->result) ) {
+			while ( $row = @$this->result->fetch_object() ) {
 				$this->last_result[$num_rows] = $row;
 				$num_rows++;
 			}

-			@mysql_free_result($this->result);
+			// no more need for freeing results, they ARE free! :D
+			// @mysql_free_result($this->result);

 			// Log number of rows the query returned
 			$this->num_rows = $num_rows;
@@ -1138,4 +1148,5 @@
 	 */
 	$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
 }
+
 ?>

You might also want to look at these posts

  • mysqli for Wordpress 2.9.1
  • javascript str_replace equivalent
  • Wordpress theme: Cloudoon
  • Google competing with code search sites
  • Postcodes van alle gemeentes van België met GPS coördinaten
  • 

    Leave a Reply

    Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Archives

    Twitter!