I'm loading items with Ajax, and I use an offset
JS:
var offset = 0;
$(".load-more").click(function() {
offset+=5;
$(this).addClass("spin");
$.ajax({
url: '/api/fetchitems.php?offset='+offset+'&sort='+sort,
success: function(data){
$(".load-more").before(data);
}
});
});
PHP/SQL:
$offset = intval($_GET["offset"]);
$stmt = $db->prepare(
"SELECT s.id,s.date,s.title,s.views,s.image,s.hidpi,s.width,s.description,u.display_name,u.avatar, s.hotness
FROM showcase AS s
INNER JOIN users AS u ON s.user_id = u.id
UNION ALL
SELECT q.id,q.date,q.title,q.views,0,0,0,q.text,u.display_name,u.avatar, q.hotness
FROM questions AS q
INNER JOIN users AS u ON q.user_id = u.id
ORDER BY hotness DESC
LIMIT :skip, 5
");
$stmt->bindParam(":skip",$offset);
$stmt->execute();
$items = $stmt->fetchAll();
$stmt = null;
I'm getting this error at execute:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''5', 5' at line 9' in **** Stack trace: #0 **** PDOStatement->execute() #1 {main} thrown in
When I run the SQL in phpMyAdmin, it works fine.