【问题标题】:How do I parse URL query into mysql database? using node.js and express如何将 URL 查询解析到 mysql 数据库中?使用 node.js 和 express
【发布时间】:2020-05-18 01:15:16
【问题描述】:

我必须将 url 查询中的名称和宽度等信息存储到我的 mysql 数据库中,并且 url 查询应该看起来像这样

/register?name=XXXX&width=###.###

我想我无法理解通过 URL 查询发送的代码获取输入的初始过程。

这是我的 /register 部分代码。任何帮助表示赞赏。

    const express = require('express');
    const mysql = require('mysql');
    const moment = require('moment');
    var cookieParser = require('cookie-parser');
    var Cookies = require('cookies');

    const db = mysql.createConnection({
      host     :     'localhost',
      user     :     'root',
      password :     'somepassword',
      database :     'somedatabase'
    })

const app = express();

app.use(express.cookieParser());

app.get('/register', (req, res) => {

})

【问题讨论】:

标签: javascript mysql node.js express


【解决方案1】:

为此,您需要 mysql 包中的 Performing Queries 文档和 express 中的 request.query

app.get('/register', (req, res) => {
  connection.query(
    'SELECT fieldA, fieldB, fieldC FROM yourTable WHERE author = ? and width = ?',
    req.query.author,
    req.query.width,
    function (error, results, fields) {
      // error will be an Error if one occurred during the query
      // results will contain the results of the query
      // fields will contain information about the returned results fields (if any)
    })
})

【讨论】:

    猜你喜欢
    • 2019-09-18
    • 2016-05-29
    • 2018-08-09
    • 2017-04-06
    • 2022-06-15
    • 2017-10-19
    • 2013-08-19
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多