MySQL: Difference between revisions
Kostya nad (talk | contribs) (Created page with " == Plugin == MySQL is a NodeJS plugin. You must download NodeJS recommend version fron [https://nodejs.org/en/ official site (https://nodejs.org/en/)] and install it with <co...") |
Kostya nad (talk | contribs) (→Plugin) |
||
| Line 8: | Line 8: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
So. Functions for working with | So. Functions for working with MySQL: | ||
=== createConnection === | === createConnection === | ||
Create information for connection but do not open it. | Create information for connection but do not open it. | ||
Revision as of 09:39, 1 November 2017
Plugin
MySQL is a NodeJS plugin. You must download NodeJS recommend version fron official site (https://nodejs.org/en/) and install it with
npm install mysql
OK. We installed MySQL. In our index.js write this:
var mysql = require('mysql');
So. Functions for working with MySQL:
createConnection
Create information for connection but do not open it.
var mysqlc = mysql.createConnection({
host:'127.0.0.1',// host of server
user:'kostya_nad',// MySQL user
password:'MyStrongPassword',// MySQL password
database:'copsandnotacops'// MySQL database
});
connect
Open connection to database
mysqlc.connect(function(e) {
if(e) {
console.log("Error connecting to the database ... ");
throw err;
}
else {
console.log('Database connected!')
}
});
end
Close connection to database
mysqlc.end();
query
Send a MySQL query to database
mysqlc.query("SELECT * FROM `users` WHERE `nickname`=", [], function(e, r) {
if(e) {
console.log('Error on connection ... ');
throw e;
}
else {
console.log('Password is '+r[0].passcode);
}
});