<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[16320] trunk/wp-includes/wp-db.php:
  Memory usage and execution improvements in wpdb.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/16320">16320</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-11-12 10:15:18 +0000 (Fri, 12 Nov 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Memory usage and execution improvements in wpdb. Store and work with resources directly, rather than full copies of results. Plugins which incorrectly used wpdb-&gt;last_result (a private property) will need to shift to wpdb-&gt;get_results() with no \$query. Magic getter is introduced for back compat when using PHP5. props joelhardi, fixes <a href="http://trac.wordpress.org/ticket/12257">#12257</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludeswpdbphp">trunk/wp-includes/wp-db.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludeswpdbphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/wp-db.php (16319 => 16320)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/wp-db.php        2010-11-12 09:49:34 UTC (rev 16319)
+++ trunk/wp-includes/wp-db.php        2010-11-12 10:15:18 UTC (rev 16320)
</span><span class="lines">@@ -125,13 +125,13 @@
</span><span class="cx">         var $last_query;
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Results of the last query made
</del><ins>+         * MySQL result resource of the last query made
</ins><span class="cx">          *
</span><del>-         * @since 1.0.0
</del><ins>+         * @since 3.1.0
</ins><span class="cx">          * @access private
</span><del>-         * @var array|null
</del><ins>+         * @var resource|null
</ins><span class="cx">          */
</span><del>-        var $last_result;
</del><ins>+        var $_mysql_last_result;
</ins><span class="cx"> 
</span><span class="cx">         /**
</span><span class="cx">          * Saved info on the table column
</span><span class="lines">@@ -1015,7 +1015,7 @@
</span><span class="cx">          * @return void
</span><span class="cx">          */
</span><span class="cx">         function flush() {
</span><del>-                $this-&gt;last_result = array();
</del><ins>+                @mysql_free_result( $this-&gt;_mysql_last_result );
</ins><span class="cx">                 $this-&gt;col_info    = null;
</span><span class="cx">                 $this-&gt;last_query  = null;
</span><span class="cx">         }
</span><span class="lines">@@ -1085,7 +1085,7 @@
</span><span class="cx">                 if ( defined( 'SAVEQUERIES' ) &amp;&amp; SAVEQUERIES )
</span><span class="cx">                         $this-&gt;timer_start();
</span><span class="cx"> 
</span><del>-                $this-&gt;result = @mysql_query( $query, $this-&gt;dbh );
</del><ins>+                $this-&gt;_mysql_last_result = @mysql_query( $query, $this-&gt;dbh );
</ins><span class="cx">                 $this-&gt;num_queries++;
</span><span class="cx"> 
</span><span class="cx">                 if ( defined( 'SAVEQUERIES' ) &amp;&amp; SAVEQUERIES )
</span><span class="lines">@@ -1107,22 +1107,15 @@
</span><span class="cx">                         $return_val = $this-&gt;rows_affected;
</span><span class="cx">                 } else {
</span><span class="cx">                         $i = 0;
</span><del>-                        while ( $i &lt; @mysql_num_fields( $this-&gt;result ) ) {
-                                $this-&gt;col_info[$i] = @mysql_fetch_field( $this-&gt;result );
</del><ins>+                        while ( $i &lt; @mysql_num_fields( $this-&gt;_mysql_last_result ) ) {
+                                $this-&gt;col_info[$i] = @mysql_fetch_field( $this-&gt;_mysql_last_result );
</ins><span class="cx">                                 $i++;
</span><span class="cx">                         }
</span><del>-                        $num_rows = 0;
-                        while ( $row = @mysql_fetch_object( $this-&gt;result ) ) {
-                                $this-&gt;last_result[$num_rows] = $row;
-                                $num_rows++;
-                        }
</del><span class="cx"> 
</span><del>-                        @mysql_free_result( $this-&gt;result );
-
</del><span class="cx">                         // Log number of rows the query returned
</span><span class="cx">                         // and return number of rows selected
</span><del>-                        $this-&gt;num_rows = $num_rows;
-                        $return_val     = $num_rows;
</del><ins>+                        $this-&gt;num_rows = @mysql_num_rows( $this-&gt;_mysql_last_result );
+                        $return_val     = $this-&gt;num_rows;
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 return $return_val;
</span><span class="lines">@@ -1281,9 +1274,10 @@
</span><span class="cx">                 if ( $query )
</span><span class="cx">                         $this-&gt;query( $query );
</span><span class="cx"> 
</span><del>-                // Extract var out of cached results based x,y vals
-                if ( !empty( $this-&gt;last_result[$y] ) ) {
-                        $values = array_values( get_object_vars( $this-&gt;last_result[$y] ) );
</del><ins>+                // Extract var from result resource based x,y vals
+                if ( $this-&gt;num_rows &gt; $y ) {
+                        @mysql_data_seek( $this-&gt;_mysql_last_result, $y );
+                        $values = @mysql_fetch_row( $this-&gt;_mysql_last_result );
</ins><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 // If there is a value return it else return null
</span><span class="lines">@@ -1310,15 +1304,17 @@
</span><span class="cx">                 else
</span><span class="cx">                         return null;
</span><span class="cx"> 
</span><del>-                if ( !isset( $this-&gt;last_result[$y] ) )
</del><ins>+                if ( $this-&gt;num_rows &lt;= $y )
</ins><span class="cx">                         return null;
</span><span class="cx"> 
</span><ins>+                @mysql_data_seek( $this-&gt;_mysql_last_result, $y );
+
</ins><span class="cx">                 if ( $output == OBJECT ) {
</span><del>-                        return $this-&gt;last_result[$y] ? $this-&gt;last_result[$y] : null;
</del><ins>+                        return @mysql_fetch_object( $this-&gt;_mysql_last_result );
</ins><span class="cx">                 } elseif ( $output == ARRAY_A ) {
</span><del>-                        return $this-&gt;last_result[$y] ? get_object_vars( $this-&gt;last_result[$y] ) : null;
</del><ins>+                        return @mysql_fetch_assoc( $this-&gt;_mysql_last_result );
</ins><span class="cx">                 } elseif ( $output == ARRAY_N ) {
</span><del>-                        return $this-&gt;last_result[$y] ? array_values( get_object_vars( $this-&gt;last_result[$y] ) ) : null;
</del><ins>+                        return @mysql_fetch_row( $this-&gt;_mysql_last_result );
</ins><span class="cx">                 } else {
</span><span class="cx">                         $this-&gt;print_error(/*WP_I18N_DB_GETROW_ERROR*/&quot; \$db-&gt;get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N&quot;/*/WP_I18N_DB_GETROW_ERROR*/);
</span><span class="cx">                 }
</span><span class="lines">@@ -1343,8 +1339,13 @@
</span><span class="cx"> 
</span><span class="cx">                 $new_array = array();
</span><span class="cx">                 // Extract the column values
</span><del>-                for ( $i = 0, $j = count( $this-&gt;last_result ); $i &lt; $j; $i++ ) {
-                        $new_array[$i] = $this-&gt;get_var( null, $x, $i );
</del><ins>+                @mysql_data_seek( $this-&gt;_mysql_last_result, 0 );
+                for ( $i = 0, $j = $this-&gt;num_rows; $i &lt; $j; $i++ ) {
+                        $values = @mysql_fetch_row( $this-&gt;_mysql_last_result );
+                        if ( isset( $values[$x] ) &amp;&amp; $values[$x] !== '' )
+                                $new_array[$i] = $values[$x];
+                        else
+                                $new_array[$i] = null;                        
</ins><span class="cx">                 }
</span><span class="cx">                 return $new_array;
</span><span class="cx">         }
</span><span class="lines">@@ -1371,32 +1372,34 @@
</span><span class="cx">                         return null;
</span><span class="cx"> 
</span><span class="cx">                 $new_array = array();
</span><ins>+                @mysql_data_seek( $this-&gt;_mysql_last_result, 0 );
</ins><span class="cx">                 if ( $output == OBJECT ) {
</span><span class="cx">                         // Return an integer-keyed array of row objects
</span><del>-                        return $this-&gt;last_result;
</del><ins>+                        for ( $i = 0, $j = $this-&gt;num_rows; $i &lt; $j; $i++ ) {
+                                $new_array[] = @mysql_fetch_object( $this-&gt;_mysql_last_result );
+                        }
+                        return $new_array;
</ins><span class="cx">                 } elseif ( $output == OBJECT_K ) {
</span><span class="cx">                         // Return an array of row objects with keys from column 1
</span><span class="cx">                         // (Duplicates are discarded)
</span><del>-                        foreach ( $this-&gt;last_result as $row ) {
</del><ins>+                        while ( $row = @mysql_fetch_object( $this-&gt;_mysql_last_result ) ) {
</ins><span class="cx">                                 $key = array_shift( $var_by_ref = get_object_vars( $row ) );
</span><span class="cx">                                 if ( ! isset( $new_array[ $key ] ) )
</span><span class="cx">                                         $new_array[ $key ] = $row;
</span><span class="cx">                         }
</span><span class="cx">                         return $new_array;
</span><del>-                } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
-                        // Return an integer-keyed array of...
-                        if ( $this-&gt;last_result ) {
-                                foreach( (array) $this-&gt;last_result as $row ) {
-                                        if ( $output == ARRAY_N ) {
-                                                // ...integer-keyed row arrays
-                                                $new_array[] = array_values( get_object_vars( $row ) );
-                                        } else {
-                                                // ...column name-keyed row arrays
-                                                $new_array[] = get_object_vars( $row );
-                                        }
-                                }
</del><ins>+                } elseif ( $output == ARRAY_A ) {
+                        // Return an integer-keyed array of column name-keyed row arrays
+                        for ( $i = 0, $j = $this-&gt;num_rows; $i &lt; $j; $i++ ) {
+                                $new_array[] = @mysql_fetch_assoc( $this-&gt;_mysql_last_result );
</ins><span class="cx">                         }
</span><span class="cx">                         return $new_array;
</span><ins>+                } elseif ( $output == ARRAY_N ) {
+                        // Return an integer-keyed array of integer-keyed row arrays
+                        for ( $i = 0, $j = $this-&gt;num_rows; $i &lt; $j; $i++ ) {
+                                $new_array[] = @mysql_fetch_row( $this-&gt;_mysql_last_result );
+                        }
+                        return $new_array;
</ins><span class="cx">                 }
</span><span class="cx">                 return null;
</span><span class="cx">         }
</span><span class="lines">@@ -1559,6 +1562,20 @@
</span><span class="cx">         function db_version() {
</span><span class="cx">                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this-&gt;dbh ) );
</span><span class="cx">         }
</span><ins>+
+        /**
+         * Magic getter used for the deprecated last_result property.
+         *
+         * @since 3.1.0
+         * @access private
+         */
+        function __get( $name ) {
+                if ( 'last_result' == $name ) {
+                        _deprecated_argument( 'wpdb', '3.1', __( 'The last_result property is deprecated. Use $wpdb-&gt;get_result().' ) );
+                        return $this-&gt;get_results();
+                }
+                return null;
+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> ?&gt;
</span></span></pre>
</div>
</div>

</body>
</html>