Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use the Wordpress function $wpdb->get_results()

https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

It says:

"If no matching rows are found, or if there is a database error, the return value will be an empty array."

Then how can I know if the query failed OR if it's empty?

like image 872
Jens Törnell Avatar asked Mar 26 '15 08:03

Jens Törnell


2 Answers

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 ();
like image 99
Sagar Devkota Avatar answered Nov 12 '22 22:11

Sagar Devkota


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.

like image 30
Jonathon Avatar answered Nov 12 '22 22:11

Jonathon