Simple $wpdb error handling

For my own copy and paste pleasures, here are some wpdb error handling examples. It will grow over time:

A very simple non-failing method for in a function:


if ($wpdb->query($query) === FALSE) {
	return FALSE;
} else {
	return $wpdb->get_results($query);
}

A more complex method for in a function. It uses WordPress’s built in WP_Error class:


if ( false === $wpdb->query($sql)) {
	if ( $wp_error ) {
		return new WP_Error( 'db_query_error', 
			__( 'Could not execute query' ), $wpdb->last_error );
	} else {
		return 0;
	}
}

A very simple flow breaking method, in-line procedural:


if ($wpdb->query($query) === FALSE) {
	wp_die( __('Crap! well that’s screwed up: ' . $wpdb->last_error) ); 
}