suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript - node查询MySQL时报错 ER_PARSE_ERROR

用express写的一个node脚本:

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

app.get('/shorten', function (req, res) {
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
        host     : '121.42.209.162',
        user     : 'root',
        password : 'password',
        database : 'mydatabase'
    });

    connection.connect(function(err) {
        if (err) {
            console.error('error connecting: ' + err.stack);
            return;
        }
        console.log('connected as id ' + connection.threadId);
    });

    var long=req.query.long;
    console.log(long);


    connection.query("SELECT * FROM url WHERE long='"+long+"'", function(err, rows) {
        if(err){
            console.log(err.code); // 'ECONNREFUSED'
            console.log(err.fatal); // true
            res.send("fail");
        }else{
            res.send("ok");
        }

    });

    connection.end();
});

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
});

运行起来了以后,用浏览器打开http://localhost:3000/shorten?long=test
浏览器里显示的是fail

服务器日志里面报错

PHP中文网PHP中文网2785 Tage vor567

Antworte allen(1)Ich werde antworten

  • 大家讲道理

    大家讲道理2017-04-10 17:07:17

    long是数据库保留字吧, 设字段的时候搞个前缀试试

    Antwort
    0
  • StornierenAntwort