I want to show ORDER BY id DESC
from: WHERE uid=1
and set LIMIT
for pagination.
Here in first code it works fine and I can sort rows by id DESC but it selects orders for ALL users:
$rows = $wpdb->get_results( "SELECT * FROM wp_orders ORDER BY id DESC LIMIT ${offset}, ${items_per_page}" );
This line below will get all rows for uid=1 but the are not sorted.
$rows = $wpdb->get_results( "SELECT * FROM wp_orders WHERE uid=1 LIMIT $offset, $items_per_page" );
The both queries above are working as expected but I can not combine them to 1.
I tried this 2 and many other options:
$rows = $wpdb->get_results( "SELECT * FROM wp_orders WHERE uid=1 ORDER BY id DESC LIMIT ${offset}, ${items_per_page}" );
$rows = $wpdb->get_results( "SELECT * FROM wp_orders WHERE uid=1 ORDER BY id DESC AND LIMIT ${offset}, ${items_per_page}" );
The 1. query gives: ERROR 502
The 2. query gives: Syntax error
FastCGI sent in stderr: "PHP message: WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND LIMIT 0, 100' at line 1 for query SELECT * FROM wp_orders WHERE uid=1 ORDER BY id DESC AND LIMIT 0, 100 made by require
I followed this: Answer and now got ERROR 502 or Syntax error.
How can I combine this 2 queries to 1?