SoFunction
Updated on 2025-03-09

nodejs mysql method to implement paging

I have learned nodejs mysql to implement paging in the past two days, which is very important, so I will add a little note today.

The code is as follows

var express = require('express');
var router = ();
var settings = require('../');
var mysql = require('mysql2');

('/', function(req, res, next) {
  var current_page = 1; //Default is 1  var num = 9; // Number of pages  if () {
    current_page = parseInt();
  }

  var last_page = current_page - 1;
  if (current_page <= 1) {
    last_page = 1;
  }
  var next_page = current_page + 1;
  var str = 'SELECT left(paragraph,50) as paragraph,date,id FROM notice limit ' + num + ' offset ' + num * (current_page - 1);
  var conn = ();

  ();
  (str, function(err, rows, fields) {
    if (err) {
      ('error', 'There is an error in data query');
    }
    if (!err) {
      if (!rows[0]) {
        ('error', 'It has reached the last page, please return');
      }
      ('notice', {
        last_page: last_page,
        next_page: next_page,
        current_page: current_page,
        mes: rows,
        error: ('error').toString()
      });

    }
  });
  ();
});

 = router;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.