Wednesday 29 June 2016

php - SQL: Syntax error or access violation

Before I start, I looked at many other posts but none have actually helped.



I get an "SQLSTATE[42000]: Syntax error or access violation" error. This error is being cause by this line:




$sql2 = $database->query("SELECT * FROM `users` LIMIT $limit");


$limit is:



$limit = ($page_id-1)*$itemsPerPage.','.$itemsPerPage;


My full error:





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 '-10,10' at line 1' in C:\xampp\htdocs\pdo\index.php:76 Stack trace: #0 C:\xampp\htdocs\pdo\index.php(76): PDO->query('SELECT * FROM `...') #1 {main} thrown in C:\xampp\htdocs\pdo\index.php on line 76




I've spent so long trying to work out this problem. I've tried executing the query without $limit. But that does not work.



Heres how I'm doing the for loop:



$limit = ($page_id-1)*$itemsPerPage.','.$itemsPerPage;

$sql2 = $database->query("SELECT * FROM `users` LIMIT $limit");

while($row = $sql2->fetch(PDO::FETCH_OBJ)) {
//My stuff in here
}


If theres any sort of way someone could help, that'd be great. Thanks!

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...