简体   繁体   English

如何使用 Node.js 通过单击按钮获取下一行数据库结果?

[英]How do I get the next row of database results from a button click using Node.js?

I'm using Node.js to serve my webpage and get the database results from mysql.我正在使用 Node.js 为我的网页提供服务并从 mysql 获取数据库结果。 Right now I have it successfully connected and showing the first row of data.(I'm using these as button values in the html code below) However, I want the user to be able to click a button and get the next row of data.现在我已成功连接并显示第一行数据。(我在下面的 html 代码中将这些用作按钮值)但是,我希望用户能够单击按钮并获取下一行数据.

var express = require("express");
var app = express();
var mysql = require('mysql');

var con = mysql.createConnection({
  // working connection
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM my_table limit 10", function (err, result, fields) {
    if (err) throw err;
    else{
      sandbox.big1Value = result[0].big1;
      sandbox.big2Value = result[0].big2;
    }
  });
});

app.get('/mypage', function (req, res) {
    res.render('mypage', { dbinput: sandbox });
});

<button type="button" id="big1" onclick="myFunction(this.value)" value=<%= dbinput.big1Value %> ><%= dbinput.big1Value %> </button>
<button type="button" id="big2" onclick="myFunction(this.value)" value=<%= dbinput.big2Value %>><%= dbinput.big2Value %> </button>
<!-- I want this button to get the next set of values -->
<button type="button" id="next" onclick="doSomething()" value="Next">Next One</button>

You can do this using limit and offset in mysql From documentation您可以使用 mysql 中的限制和偏移量 从文档中执行此操作

Syntax: LIMIT {[offset,] row_count | row_count OFFSET offset}语法: LIMIT {[offset,] row_count | row_count OFFSET offset} LIMIT {[offset,] row_count | row_count OFFSET offset}

Here in your case you can use:在您的情况下,您可以使用:

SELECT * FROM my_table LIMIT 10, 10;  # Retrieve rows 11-20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果数据库中有1个节点,node.js和mysql如何得到2个? - node.js and mysql how do I get 2 if have 1 in the database? 我试图从 Node.js 中的 MySQL 数据库中获取前 3 个结果,但出现此错误 - I'm trying to get first 3 results from MySQL database in Node.js and I'm getting this error 如何等待Node.js中的下一个事件? - How do I wait for the next event in Node.js? 如何使用Google Maps API在node.js中单击按钮获得用户输入? - How to get user input on click of button in node.js using google maps api? 如何使用node.js以HTML显示来自数据库的查询结果 - How to display query results from a database in HTML using node.js 如何使用 JS 路径获得启动点击以点击下一页? - How do I get splash to click to click through next page using JS path? 如何通过按钮在node.js中渲染车把布局? - How do I render a handlebars layout in node.js from a button? 如何将 URL 查询解析为 mysql 数据库? 使用 node.js 并表达 - How do I parse URL query into mysql database? using node.js and express 如何使用Node.js在数据库中保存图像路径? - How do I save Image path in the database using Node.js? 如何使用MongoDB Node.JS驱动程序创建新数据库? - How do I create a new database using the MongoDB Node.JS driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM