0

I'm new to node js when i try to execute the below program am getting an exception as

var mysql = require('mysql');

var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : 'Gemini*123',
    database : 'test'
});

connection.connect();

var queryString = "select * user_details";

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;

    for (var i in rows) {
        console.log('Post Titles: ', rows[i].user_name);
    }
});

connection.end();

"Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user_details' at line 1"?

Thanks

3 Answers 3

2

Your query String should be var querystring = "SELECT * FROM user_details" and not SELECT * user_details

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to put 'From' on your query:

var queryString = "select * from user_details";

Comments

0

As already pointed out, your SQL is wrong - you missed out the FROM keyword from your FROM clause.

Your for loop also might result in an error

for (var i in rows) will iterate over all the properties of the object. You probably just want to iterate over the entries of the array, so you'll want to iterate over just the indices instead

for (var idx = 0; idx < rows.length; idx++) {
  var row = rows[idx];
  ...
}

or you can go functional:

rows.forEach(function (row) {
  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.