Reputation: 71
I receive the following error, the error contains a SQL syntax problem. I would not know what the problem is because everything seems fine. I can see a negative number in $range_start (-4). What would be the problem? Should I add anything at the negative number in the SQL query?
(It's a pagination and it works fine in other SQL queries)
Thanks in advance.
Error:
Array
(
[0] => 42000
[1] => 1064
[2] => 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 ''4'' at line 1
)
Code:
$getPostsByCategory = $this->db->prepare("SELECT * FROM articles WHERE category = :category ORDER by date_created DESC LIMIT " . $range_start . ", " . $range_end . "");
Upvotes: 1
Views: 680
Reputation: 77118
The LIMIT
keyword specifies that you want a subset of the elements. $range_start
is the offset, that is, the first index of the result you want, while $range_end
is the number of elements you want. As a result, both of these need to be positive integers, so $range_start
cannot be negative, as you cannot load the elements, starting from the -4th.
Upvotes: 2
Reputation: 50808
Strings need to be encapsulated by either a quote or apostrophe depending on whether your string was constructed with quotes or apostrophes.
In this case, I would recommend the following:
'" . $range_start . "', '" . $range_end . "'
Note the use of apostrophe's before and after.
Upvotes: 0