1

If i have this table on dataBase.js file:

client.query(
'CREATE TEMPORARY TABLE IF NOT EXISTS '+USER+
'(username VARCHAR(255), '+
'password VARCHAR(255), '+
'name VARCHAR(255), '+
'picture VARCHAR(255), '+
'PRIMARY KEY(username))'
);

and lets say i want to check if i have a given user already in my dataBase,

how can i get the data after running the following query? :

function checkUser(username,password){
client.query('SELECT username, password FROM ' + USER + 'WHERE username=?', [username] , 'AND password=?', [password] 
function (err, results, fields) {
        if (err) {
            throw err;
        }
 });
}

if an error occurred, it will be handled but how can get the needed data?

any help will be much appreciated!

4
  • 1
    Look in the results and fields variables. Commented Jan 24, 2012 at 3:03
  • this is just an example from some site, what does results and fields hold? Commented Jan 24, 2012 at 3:06
  • Have you tried console.log'ing them to see? :) Commented Jan 24, 2012 at 4:16
  • @nicolaskruchten no because i cant compile it. im doing something wrong over there Commented Jan 24, 2012 at 4:23

1 Answer 1

2

if there is no errors, you have your data in the results

function checkUser(username,password,haveResult) {
    client.query('SELECT username, password FROM ' + USER + 'WHERE username=? AND password=?', [username, password],  
    function (err, results, fields) {
        if (err) {
            // problems
            throw err;
        } else {
            // do something with data - it is in results array
            var checkResult = true; // here something depending on query result
            haveResult(checkResult); // continue via callback
        }
    });
}


// use it
checkUser('bar', 'baz', function(isGood) {
    console.log('user is' + (isGood? 'good' : 'bad') );
});
Sign up to request clarification or add additional context in comments.

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.