<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[27250] trunk: Use ext/mysqli in PHP 5.5 or greater.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27250">27250</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-02-25 00:39:28 +0000 (Tue, 25 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use ext/mysqli in PHP 5.5 or greater. Expect minor explosions.

props aaroncampbell, pento.
see <a href="http://core.trac.wordpress.org/ticket/21663">#21663</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeswpdbphp">trunk/src/wp-includes/wp-db.php</a></li>
<li><a href="#trunktestsphpunittestsdbphp">trunk/tests/phpunit/tests/db.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/wp-db.php (27249 => 27250)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/wp-db.php  2014-02-25 00:29:01 UTC (rev 27249)
+++ trunk/src/wp-includes/wp-db.php     2014-02-25 00:39:28 UTC (rev 27250)
</span><span class="lines">@@ -539,6 +539,15 @@
</span><span class="cx">          'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Whether to use mysqli over mysql.
+        *
+        * @since 3.9.0
+        * @access private
+        * @var bool
+        */
+       private $use_mysqli = false;
+
+       /**
</ins><span class="cx">    * Connects to the database server and selects a database
</span><span class="cx">   *
</span><span class="cx">   * PHP5 style constructor for compatibility with PHP5. Does
</span><span class="lines">@@ -559,6 +568,8 @@
</span><span class="cx">          if ( WP_DEBUG && WP_DEBUG_DISPLAY )
</span><span class="cx">                  $this->show_errors();
</span><span class="cx"> 
</span><ins>+               $this->use_mysqli = ( version_compare( phpversion(), '5.5', '>=' ) && function_exists( 'mysqli_connect' ) );
+
</ins><span class="cx">           $this->init_charset();
</span><span class="cx"> 
</span><span class="cx">          $this->dbuser = $dbuser;
</span><span class="lines">@@ -666,13 +677,24 @@
</span><span class="cx">          if ( ! isset( $collate ) )
</span><span class="cx">                  $collate = $this->collate;
</span><span class="cx">          if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
</span><del>-                       if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
-                               mysql_set_charset( $charset, $dbh );
</del><ins>+                        if ( $this->use_mysqli ) {
+                               if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
+                                       mysqli_set_charset( $dbh, $charset );
+                               } else {
+                                       $query = $this->prepare( 'SET NAMES %s', $charset );
+                                       if ( ! empty( $collate ) )
+                                               $query .= $this->prepare( ' COLLATE %s', $collate );
+                                       mysqli_query( $query, $dbh );
+                               }
</ins><span class="cx">                   } else {
</span><del>-                               $query = $this->prepare( 'SET NAMES %s', $charset );
-                               if ( ! empty( $collate ) )
-                                       $query .= $this->prepare( ' COLLATE %s', $collate );
-                               mysql_query( $query, $dbh );
</del><ins>+                                if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
+                                       mysql_set_charset( $charset, $dbh );
+                               } else {
+                                       $query = $this->prepare( 'SET NAMES %s', $charset );
+                                       if ( ! empty( $collate ) )
+                                               $query .= $this->prepare( ' COLLATE %s', $collate );
+                                       mysql_query( $query, $dbh );
+                               }
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="lines">@@ -689,12 +711,25 @@
</span><span class="cx">   */
</span><span class="cx">  function set_sql_mode( $modes = array() ) {
</span><span class="cx">          if ( empty( $modes ) ) {
</span><del>-                       $res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh );
</del><ins>+                        if ( $this->use_mysqli ) {
+                               $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
+                       } else {
+                               $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
+                       }
+
</ins><span class="cx">                   if ( empty( $res ) ) {
</span><span class="cx">                          return;
</span><span class="cx">                  }
</span><span class="cx"> 
</span><del>-                       $modes_str = mysql_result( $res, 0 );
</del><ins>+                        if ( $this->use_mysqli ) {
+                               $modes_array = mysqli_fetch_array( $res );
+                               if ( empty( $modes_array[0] ) ) {
+                                       return;
+                               }
+                               $modes_str = $modes_array[0];
+                       } else {
+                               $modes_str = mysql_result( $res, 0 );
+                       }
</ins><span class="cx"> 
</span><span class="cx">                  if ( empty( $modes_str ) ) {
</span><span class="cx">                          return;
</span><span class="lines">@@ -724,7 +759,11 @@
</span><span class="cx"> 
</span><span class="cx">          $modes_str = implode( ',', $modes );
</span><span class="cx"> 
</span><del>-               mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh );
</del><ins>+                if ( $this->use_mysqli ) {
+                       mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
+               } else {
+                       mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );
+               }
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -909,7 +948,12 @@
</span><span class="cx">          if ( is_null($dbh) )
</span><span class="cx">                  $dbh = $this->dbh;
</span><span class="cx"> 
</span><del>-               if ( !@mysql_select_db( $db, $dbh ) ) {
</del><ins>+                if ( $this->use_mysqli ) {
+                       $success = @mysqli_select_db( $dbh, $db );
+               } else {
+                       $success = @mysql_select_db( $db, $dbh );
+               }
+               if ( ! $success ) {
</ins><span class="cx">                   $this->ready = false;
</span><span class="cx">                  wp_load_translations_early();
</span><span class="cx">                  $this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
</span><span class="lines">@@ -945,8 +989,9 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Real escape, using mysql_real_escape_string()
</del><ins>+         * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
</ins><span class="cx">    *
</span><ins>+        * @see mysqli_real_escape_string()
</ins><span class="cx">    * @see mysql_real_escape_string()
</span><span class="cx">   * @since 2.8.0
</span><span class="cx">   * @access private
</span><span class="lines">@@ -955,8 +1000,13 @@
</span><span class="cx">   * @return string escaped
</span><span class="cx">   */
</span><span class="cx">  function _real_escape( $string ) {
</span><del>-               if ( $this->dbh )
-                       return mysql_real_escape_string( $string, $this->dbh );
</del><ins>+                if ( $this->dbh ) {
+                       if ( $this->use_mysqli ) {
+                               return mysqli_real_escape_string( $this->dbh, $string );
+                       } else {
+                               return mysql_real_escape_string( $string, $this->dbh );
+                       }
+               }
</ins><span class="cx"> 
</span><span class="cx">          $class = get_class( $this );
</span><span class="cx">          _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE );
</span><span class="lines">@@ -1102,8 +1152,13 @@
</span><span class="cx">  function print_error( $str = '' ) {
</span><span class="cx">          global $EZSQL_ERROR;
</span><span class="cx"> 
</span><del>-               if ( !$str )
-                       $str = mysql_error( $this->dbh );
</del><ins>+                if ( !$str ) {
+                       if ( $this->use_mysqli ) {
+                               $str = mysqli_error( $this->dbh );
+                       } else {
+                               $str = mysql_error( $this->dbh );
+                       }
+               }
</ins><span class="cx">           $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
</span><span class="cx"> 
</span><span class="cx">          if ( $this->suppress_errors )
</span><span class="lines">@@ -1206,8 +1261,13 @@
</span><span class="cx">          $this->rows_affected = $this->num_rows = 0;
</span><span class="cx">          $this->last_error  = '';
</span><span class="cx"> 
</span><del>-               if ( is_resource( $this->result ) )
-                       mysql_free_result( $this->result );
</del><ins>+                if ( is_resource( $this->result ) ) {
+                       if ( $this->use_mysqli ) {
+                               mysqli_free_result( $this->result );
+                       } else {
+                               mysql_free_result( $this->result );
+                       }
+               }
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -1228,18 +1288,40 @@
</span><span class="cx">          $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
</span><span class="cx">          $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
</span><span class="cx"> 
</span><del>-               if ( WP_DEBUG ) {
-                       $error_reporting = false;
-                       if ( defined( 'E_DEPRECATED' ) ) {
-                               $error_reporting = error_reporting();
-                               error_reporting( $error_reporting ^ E_DEPRECATED );
</del><ins>+                if ( $this->use_mysqli ) {
+                       $this->dbh = mysqli_init();
+
+                       // mysqli_real_connect doesn't support the host param including a port or socket
+                       // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
+                       $port = null;
+                       $socket = null;
+                       $host = $this->dbhost;
+                       $port_or_socket = strstr( $host, ':' );
+                       if ( ! empty( $port_or_socket ) ) {
+                               $host = strstr( $host, ':', true );
+                               $port_or_socket = substr( $port_or_socket, 1 );
+                               if ( 0 !== strpos( $port_or_socket, '/' ) ) {
+                                       $port = intval( $port_or_socket );
+                                       $maybe_socket = strstr( $port_or_socket, ':' );
+                                       if ( ! empty( $maybe_socket ) ) {
+                                               $socket = substr( $maybe_socket, 1 );
+                                       }
+                               } else {
+                                       $socket = $port_or_socket;
+                               }
</ins><span class="cx">                   }
</span><del>-                       $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
-                       if ( false !== $error_reporting ) {
-                               error_reporting( $error_reporting );
</del><ins>+
+                       if ( WP_DEBUG ) {
+                               mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
+                       } else {
+                               @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
</ins><span class="cx">                   }
</span><span class="cx">          } else {
</span><del>-                       $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
</del><ins>+                        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 );
+                       }
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          if ( ! $this->dbh && $allow_bail ) {
</span><span class="lines">@@ -1285,8 +1367,14 @@
</span><span class="cx">   * @return bool True if the connection is up.
</span><span class="cx">   */
</span><span class="cx">  function check_connection() {
</span><del>-               if ( @mysql_ping( $this->dbh ) ) {
-                       return true;
</del><ins>+                if ( $this->use_mysqli ) {
+                       if ( @mysqli_ping( $this->dbh ) ) {
+                               return true;
+                       }
+               } else {
+                       if ( @mysql_ping( $this->dbh ) ) {
+                               return true;
+                       }
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          $error_reporting = false;
</span><span class="lines">@@ -1368,14 +1456,29 @@
</span><span class="cx">          $this->_do_query( $query );
</span><span class="cx"> 
</span><span class="cx">          // MySQL server has gone away, try to reconnect
</span><del>-               if ( empty( $this->dbh ) || 2006 == mysql_errno( $this->dbh ) ) {
</del><ins>+                $mysql_errno = 0;
+               if ( ! empty( $this->dbh ) ) {
+                       if ( $this->use_mysqli ) {
+                               $mysql_errno = mysqli_errno( $this->dbh );
+                       } else {
+                               $mysql_errno = mysql_errno( $this->dbh );
+                       }
+               }
+
+               if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
</ins><span class="cx">                   if ( $this->check_connection() ) {
</span><span class="cx">                          $this->_do_query( $query );
</span><span class="cx">                  }
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          // If there is an error then take note of it..
</span><del>-               if ( $this->last_error = mysql_error( $this->dbh ) ) {
</del><ins>+                if ( $this->use_mysqli ) {
+                       $this->last_error = mysqli_error( $this->dbh );
+               } else {
+                       $this->last_error = mysql_error( $this->dbh );
+               }
+
+               if ( $this->last_error ) {
</ins><span class="cx">                   // Clear insert_id on a subsequent failed insert.
</span><span class="cx">                  if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
</span><span class="cx">                          $this->insert_id = 0;
</span><span class="lines">@@ -1387,18 +1490,33 @@
</span><span class="cx">          if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) {
</span><span class="cx">                  $return_val = $this->result;
</span><span class="cx">          } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) {
</span><del>-                       $this->rows_affected = mysql_affected_rows( $this->dbh );
</del><ins>+                        if ( $this->use_mysqli ) {
+                               $this->rows_affected = mysqli_affected_rows( $this->dbh );
+                       } else {
+                               $this->rows_affected = mysql_affected_rows( $this->dbh );
+                       }
</ins><span class="cx">                   // Take note of the insert_id
</span><span class="cx">                  if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
</span><del>-                               $this->insert_id = mysql_insert_id($this->dbh);
</del><ins>+                                if ( $this->use_mysqli ) {
+                                       $this->insert_id = mysqli_insert_id( $this->dbh );
+                               } else {
+                                       $this->insert_id = mysql_insert_id( $this->dbh );
+                               }
</ins><span class="cx">                   }
</span><span class="cx">                  // Return number of rows affected
</span><span class="cx">                  $return_val = $this->rows_affected;
</span><span class="cx">          } else {
</span><span class="cx">                  $num_rows = 0;
</span><del>-                       while ( $row = @mysql_fetch_object( $this->result ) ) {
-                               $this->last_result[$num_rows] = $row;
-                               $num_rows++;
</del><ins>+                        if ( $this->use_mysqli ) {
+                               while ( $row = @mysqli_fetch_object( $this->result ) ) {
+                                       $this->last_result[$num_rows] = $row;
+                                       $num_rows++;
+                               }
+                       } else {
+                               while ( $row = @mysql_fetch_object( $this->result ) ) {
+                                       $this->last_result[$num_rows] = $row;
+                                       $num_rows++;
+                               }
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><span class="cx">                  // Log number of rows the query returned
</span><span class="lines">@@ -1425,7 +1543,11 @@
</span><span class="cx">                  $this->timer_start();
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $this->result = @mysql_query( $query, $this->dbh );
</del><ins>+                if ( $this->use_mysqli ) {
+                       $this->result = @mysqli_query( $this->dbh, $query );
+               } else {
+                       $this->result = @mysql_query( $query, $this->dbh );
+               }
</ins><span class="cx">           $this->num_queries++;
</span><span class="cx"> 
</span><span class="cx">          if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
</span><span class="lines">@@ -1763,8 +1885,14 @@
</span><span class="cx">          if ( $this->col_info )
</span><span class="cx">                  return;
</span><span class="cx"> 
</span><del>-               for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
-                       $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
</del><ins>+                if ( $this->use_mysqli ) {
+                       for ( $i = 0; $i < @mysqli_num_fields( $this->result ); $i++ ) {
+                               $this->col_info[ $i ] = @mysqli_fetch_field( $this->result, $i );
+                       }
+               } else {
+                       for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) {
+                               $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i );
+                       }
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -1936,6 +2064,11 @@
</span><span class="cx">   * @return false|string false on failure, version number on success
</span><span class="cx">   */
</span><span class="cx">  function db_version() {
</span><del>-               return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
</del><ins>+                if ( $this->use_mysqli ) {
+                       $server_info = mysqli_get_server_info( $this->dbh );
+               } else {
+                       $server_info = mysql_get_server_info( $this->dbh );
+               }
+               return preg_replace( '/[^0-9.].*/', '', $server_info );
</ins><span class="cx">   }
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsdbphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/db.php (27249 => 27250)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/db.php 2014-02-25 00:29:01 UTC (rev 27249)
+++ trunk/tests/phpunit/tests/db.php    2014-02-25 00:39:28 UTC (rev 27250)
</span><span class="lines">@@ -50,7 +50,11 @@
</span><span class="cx">          $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
</span><span class="cx">          $this->assertGreaterThan( 0, $var );
</span><span class="cx"> 
</span><del>-               mysql_close( $wpdb->dbh );
</del><ins>+                if ( $wpdb->use_mysqli ) {
+                       mysqli_close( $wpdb->dbh );
+               } else {
+                       mysql_close( $wpdb->dbh );
+               }
</ins><span class="cx">           unset( $wpdb->dbh );
</span><span class="cx"> 
</span><span class="cx">          $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
</span></span></pre>
</div>
</div>

</body>
</html>