mysql - 无法使用node-mysql分页

标签 mysql node.js node-mysql

所以在我的服务器中我有:

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/

相关文章:

mysql - 在mysql中创建一个关于自身的表关系

javascript - 使用 node-mysql 同时查询

javascript - 需要来自另一个文件的函数和变量=nodejs

javascript - 如果元素是由指令生成的,ng-click 不起作用

javascript - JavaScript 中的赋值和 var 关键字

mysql - Node mysql 错误 : connect ECONNREFUSED

mysql - 如何在ServiceStack ormlite中自动关闭mysql连接

sql - Mysql索引被忽略

mysql - 围绕唯一数据对 MySQL 表进行分区

node.js - 在 node-csv-parser : RangeError: Maximum call stack size exceeded 中调用 node-mongodb-native