Manish
Manish

Reputation: 328

Connect Node with mysqli module?

I want to connect Node JavaScript with Mysqli. I have downloaded the mysqli module using following command.

npm install mysqli

And Then Create JavaScript file with following code.

var Mysqli = require('mysqli');

// incoming json 
let  conn  = new Mysqli ( {  
      host: "localhost",
      user: "root",
     password: "",
     database: "ll"
    } );

conn.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM post", function (err, result) {
    if (err) throw err;
    console.log(result);
  });
});
 

But not able to connect to database.

I have used following Packages.

https://www.npmjs.com/package/mysqli

Upvotes: 0

Views: 4646

Answers (3)

David R
David R

Reputation: 15667

The module which you're using seems to be a newer one and which is not stable.

Consider using mysql module which has been accepted by many developers, it has a great documentation and an active github community + google mailing list and IRC channel.

Upvotes: 0

GodsArchitect
GodsArchitect

Reputation: 61

You have a typo here:

conn.query

Unless, of course, you wrote the code here again. I am sure you would have figured that out by now, but still... After all, this is the first thing that comes up when someone searches for mysqli nodejs.

Upvotes: 1

HubballiHuli
HubballiHuli

Reputation: 777

If connecting to mysql db is the goal, then I suggest you to use mysql module.
install mysql module by running npm install mysql
below is the sample code for how to use the mysql module

const mysql  = require('mysql');
const conn = mysql.createConnection(
{
    host:'your_host',             //localhost in your case
    user:'db_user',               // root in your case
    password: 'password',         //blank string in your case
    database:'your_db_name'       //'ll' in your case
});

//executing the queries
conn.query('SELECT * from post',function(err,result){ //result of the query is stored in 'result' and the error, if any, are stored in err
    if(err)
    {
        console.log(err);
    }
    else
    {
         console.log(result);
    }
});

click this link for further information

Upvotes: 0

Related Questions