Skip to main content
4 of 7
added 60 characters in body
birgire
  • 67.8k
  • 7
  • 119
  • 251

You have the following methods of the $wpdb object:

  • $wpdb->flush() that contains a call to mysqli_free_result() or mysql_free_result() if not supported.

  • $wpdb->check_connection() that contains a call to mysqli_ping() or mysql_ping() if not supported.

You can of course use all your PHP functions in WordPress.

Here's an untested example:

// Use the global instance created by WordPress
global $wpdb;

// Fetch our data with some huge query:
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->posts}" );    

// ... some data handling here

// Let's flush for another huge query.
// But that's not actually needed, 
// since this is already done in the $wpdb->query() call
// that's used within the $wpdb->get_results() method.
$wpdb->flush();

// Check the connection:
if( ! $wpdb->check_connection( $allow_bail = false ) ) 
{
   // Let's try to connect again:
   if( ! $wpdb->db_connect( $allow_bail = false ) )
   {
        wp_die( wp_sprintf( '<pre>%s<pre>', __( 'No DB connection' ) ) );
   }        
}

// Fetch another set of data:
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->users}" );    

Notice that in most cases, we can simple use WP_Query, WP_Comment_Query or WP_User_Query and don't need to create custom SQL queries with $wpdb.

birgire
  • 67.8k
  • 7
  • 119
  • 251