简体   繁体   English

如何将 URL 查询解析为 mysql 数据库? 使用 node.js 并表达

[英]How do I parse URL query into mysql database? using node.js and express

I have to store information such as name and width from the url query to my mysql database and the url query should look something like this我必须将 url 查询中的名称和宽度等信息存储到我的 mysql 数据库中,并且 url 查询应该如下所示

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

I guess I'm having trouble understanding the initial process of taking the inputs through my code that is sent through the URL query.我想我无法理解通过 URL 查询发送的代码获取输入的初始过程。

Here is my /register portion of my code.这是我的代码的 /register 部分。 Any help is appreciated.任何帮助表示赞赏。

    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) => {

})

For that you will need the Performing Queries documentation from the mysql package and the request.query from express.为此,您需要mysql package 中的执行查询文档和 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)
    })
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM