Wordpress - get_results() - How to know if failed or empty? Wordpress - get_results() - How to know if failed or empty? wordpress wordpress

Wordpress - get_results() - How to know if failed or empty?


Use

$results=$wpdb->get_results($yoursql);if (count($results)> 0){    //do here}

But if you want to know if query failed

$wpdb -> show_errors ();$wpdb -> get_results ($wpdb -> prepare($sql));$wpdb -> print_error ();


Bit late to the party here but I'm just looking for the same thing. I've had a browse through the wp-db.php code on version 4.4.2.

On line 1422, inside the method flush() there's a bit of code which resets the last_error property:

$this->last_error  = '';

This flush() method is called in the query() method on line 1693:

$this->flush();

The get_results() method calls query() on line 2322:

if ( $query ) {    $this->query( $query );} else {    return null;}

With this we can be pretty sure that more or less every time get_results() (Or get_row() too for that matter) is called, query() and flush() are both called, which ensures that last_error is set to the empty string before the query is executed.

So assuming the query runs (If it doesn't, null is returned - if the query is empty for example), last_error should contain an error message if the query was to fail for some reason.

Since last_error is flush()ed/reset each time, it should only contain an error for the last query that was run, rather than the last error for any query that had been run previously. With this in mind it should be safe to rely on last_error to determine whether something went wrong with the query.

$results = $wpdb->get_results($sql);if (is_null($results) || !empty($wpdb->last_error)) {    // Query was empty or a database error occurred} else {    // Query succeeded. $results could be an empty array here}

Not the most intuitive in my opinion, but it seems to be sufficient.

Personally, I've written my own class around wpdb for my own benefit. This is my getResults() method.

public function getResults($query, $bindings = []){    // Prepare the statement (My prepare method inspects $query and just returns it if there's no bindings, otherwise it uses $wpdb->prepare()            $prepared = $this->prepare($query, $bindings);    // Execute the statement    $rows = $this->db->get_results($prepared, ARRAY_A);    // If an array was returned and no errors occurred, return the result set    if (is_array($rows) && empty($this->db->last_error)) {        return $rows;    }    // On failure, return false    return false;}

Hope this helps.


Wpdb->get_results function from wordpress returns the result if successful otherwise it will return null. There can be many reasons if a query get failed.Refer in-depth article on debugging get_results() returning empty results here

Although you can use functions like wpdb->show_error() to check what was the last error after executing the sql query. sometimes this error returns emptythen try to use wpdb->last_query to check the final query that get formed.