0

I am trying to connect to MYSQL database.

My code:

 E-mail: <input type="email" id="email" size=25><br>

<button id="go" onclick="register()">Go</button>

<script type="text/javascript">

    function register(){
        var mysql = require('mysql');

    var con = mysql.createConnection({
        host: "localhost",
        user: "yourusername",
        password: "yourpassword"
    });

    con.connect(function(err) {
        if (err) throw err;
        console.log("Connected!");
    });
}
</script>

When I open console it shows:

Uncaught ReferenceError: require is not defined

I have nodejs installed and have also installed MYSQL from cmd using the command:

npm install mysql

What is wrong here?

wazz
  • 4,953
  • 5
  • 20
  • 34

1 Answers1

0

require doesn't exist on the client (at least not natively). The code you have is meant to be run on the server (node.js).

Try putting this into a file, say main.js, and then run node main.js.

var mysql = require('mysql');

var con = mysql.createConnection({
    host: "localhost",
    user: "yourusername",
    password: "yourpassword"
});

con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
});
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71