javascript - node查询MySQL时报错 ER_PARSE_ERROR
PHP中文网
PHP中文网 2017-04-10 17:05:17
0
1
564

用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中文网

认证0级讲师

reply all(1)
大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template