How to get all queries’s results after they have executed?

Extending the wpdb class

We can create the db.php file under wp-content/ directory to override the wpdb class to our needs.

I checked this site for such examples and found one by @MarkKaplun here.

Here’s an example how one can get access to the last result, after each query has run:

<?php
/**
 * db.php - Override the global $wpdb object to collect all query results
 */
namespace WPSE\Question242685;

class DB extends \wpdb
{
    public function __construct( $dbuser, $dbpassword, $dbname, $dbhost )
    {
        // Parent constructor
        parent::__construct( $dbuser, $dbpassword, $dbname, $dbhost );
    }

    public function query( $query )
    {
        // Parent query
        $val = parent::query( $query );

        //----------------------------------------------------------------------
        // Edit this to your needs
        //
        // Warning: It can be slow and resource demanding to collect all results 
        if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
        {
            // do something with $this->last_result;
        }
        //----------------------------------------------------------------------

        return $val;
    }

} // end class

// Override the global wpdb object
$GLOBALS['wpdb'] = new DB( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );

Here we assume the SAVEQUERIES is defined as true in the wp-config.php file.

Note that collecting all the results can be resource demanding, so you better only test this on a dev install.

error code: 523