nodejs mysql 实现分页的方法
这两天学习了nodejs mysql 实现分页,很重要,所以,今天添加一点小笔记。
代码如下
var express = require('express'); var router = express.Router(); var settings = require('../settings.js'); var mysql = require('mysql2'); router.get('/', function(req, res, next) { var current_page = 1; //默认为1 var num = 9; //一页条数 if (req.query.page) { current_page = parseInt(req.query.page); } 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 = mysql.createConnection(settings.db); conn.connect(); conn.query(str, function(err, rows, fields) { if (err) { req.flash('error', '数据查询有误'); } if (!err) { if (!rows[0]) { req.flash('error', '已到最后一页,请返回'); } res.render('notice', { last_page: last_page, next_page: next_page, current_page: current_page, mes: rows, error: req.flash('error').toString() }); } }); conn.end(); }); module.exports = router;
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11