所以在我的服务器中我有:
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);
});
});
当我尝试像这样访问它时:http://localhost:3000/getposts/2
我收到错误:
{ 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
已记录在控制台中。我将 LIMIT 的第一个参数作为 ?
[pageno]
传递,如其 official notes 中所述。如果我在 LIMIT 2,3
中输入 2
或任何其他数字,它就会起作用。我的表中有一千多个虚拟记录。
也尝试过
var sql = 'SELECT * FROM mytable LIMIT' + connection.escape(pageno) + ', 3';
并在 connection.query
中使用 sql
但出现同样的错误。
我做错了什么?请帮忙。
最佳答案
您可以尝试使用 parseInt()
获取页码。
关于mysql - 无法使用node-mysql分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47496572/