代码之家  ›  专栏  ›  技术社区  ›  Somename

无法使用节点mysql分页

  •  0
  • Somename  · 技术社区  · 8 年前

    因此,在我的服务器中,我有:

    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 在控制台中记录。我将极限的第一个参数传递为 ? [pageno] 如他们的 official notes 如果我键入 2 LIMIT 2,3 ,它有效。我在表中有一千多条虚拟记录。

    也尝试过 var sql = 'SELECT * FROM mytable LIMIT ' + connection.escape(pageno) + ', 3'; 并已使用 sql 在里面 connection.query 但同样的错误。

    我做错了什么?请帮忙。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Krishnadas PC    8 年前

    你可以试试 parseInt() 对于页码。