-2

I'm trying to connect to my local mysql database by using node.js on my website. I have installed node.js on my laptop. This is my code that is executed:

function registerUser()
{
    var mysql = require("mysql");

    var connection = mysql.createConnection( {
                                            host: 'localhost',
                                            user: 'root',
                                            password: '',
                                            database: 'Fountaint_News_Database',
                                            port: 3306
                                            });

    connection.connect();

    var query = connection.query('INSERT INTO registered_users VALUES ("test", 22)',
                                 function(err, result, fields) {
                                 if (err) throw err;
                                 console.log('result: ', result);
                                 });

    connection.end();

    console.log('Connection closed');
}

I haven't done anything else, I'm not sure if there is anything else to setup, but when I runt I just get this error "ReferenceError: Can't find variable: require".

I hope that someone can help me.

4

1 回答 1

1

您是在服务器端而不是在客户端运行此代码,不是吗?我的意思是将此文件保存在 .js 文件中,通过控制台运行

node yourfile.js

显然之后

npm install mysql

在同一个目录?

显然,您需要将 registerUser() 函数绑定到某个事件:)

于 2013-11-12T13:01:13.660 回答