Why is an empty result an error? ( $wpdb->get_row )

I have a query where I get the highest id from a table.

$user_id = $wpdb-get_row("SELECT * FROM $table WHERE id=(SELECT MAX(id) FROM $table)")

The table should always have rows, but if for some reason it returns empty, there is an error (printed with $wpdb-print_error()):

WordPress database error: []

I was wondering why an empty result means an error? And how would I prevent this error?

Topic get-row wpdb Wordpress

Category Web


There isn't an error. The square brackets are empty-- aka "no error". Put a spurious comma in the SQL and you will see what I mean.

But your SQL is wildly complicated for you are doing. That is entirely equivalent to

SELECT * FROM $table ORDER BY id DESC LIMIT 1

Though, I notice you are using id rather than ID and I don't remember any Core tables using the lowercase version. If this is meant for a Core table that will be a problem.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.