So in my server I have:
app.get('/getposts/:pageno', (req, res) => {
let pageno = req.params.pageno
connection.query('SELECT * FROM mytable LIMIT ? , 3', [pageno], function(err, rows, fields){
if(err){
console.log(err);
}
res.send(rows);
console.log(pageno);
});
});
And when I try to access it like: http://localhost:3000/getposts/2
I get error:
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ''2' , 3'
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: 'You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near
\'\'2\' , 3\' at line 1',
sqlState: '42000',
index: 0,
sql: 'SELECT * FROM mytable LIMIT \'2\' , 3' }
pageno
is logged in the console. I'm passing the first parameter of the LIMIT as ?
[pageno]
as described in their official notes If I type 2
or any other number in the LIMIT 2,3
, it works. I have over a thousand dummy records in the table.
Also tried
var sql = 'SELECT * FROM mytable LIMIT ' + connection.escape(pageno) + ', 3';
and used sql
in connection.query
but same error.
What am I doing wrong? Please help.
You can try parseInt()
for the page number.