twan twan - 4 months ago 28
MySQL Question

SQL OFFSET syntax error

I'm trying to increase the offset every time by 3, so I used a variable that is increased by 3 every time a button is clicked (using ajax).

However, I keep getting a syntax error when using

OFFSET
.

Is this the correct way to do it?

$web = "SELECT * FROM `db` WHERE catid = 9 AND state = 1 ORDER BY ordering LIMIT 0,".$_POST['limit']." OFFSET ".$_POST['limit']."";


limit is increased by 3 every button click, if I leave out
OFFSET
, it works, but it keeps loading all items again, instead of just 3 new items.

My error:


There was an error running the query [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 'OFFSET 3' at line 1]

Answer

The syntax should be:

 LIMIT ".$_POST['limit']." OFFSET ".$_POST['limit']."";

The 0 conflicts with OFFSET.