0

I am trying to connect to a db on my hosting and write out the result, I am not getting any error, just blank space. line like console.log('test'); put at any place always work but I am not getting any query results, what am I doing wrong?

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'wm51.wedos.net',
    user     : 'xxxxxx',
    password : 'xxxxxx',
    database: 'd57283_vs'
});

connection.connect();
var queryString = 'SELECT * FROM versus LIMIT 5';

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;
    for (var i in rows) {
        console.log(rows[i].title); 

    }
});

connection.end();

(The table is called versus, has columns title, url...in adminer it's all accessible and the query works...)

Paul Mougel
  • 16,728
  • 6
  • 57
  • 64
yadayada
  • 3
  • 1
  • 5

3 Answers3

1

Be careful with that connection.end(); call at the bottom. NodeJS is asynchronous, remember?

Try putting that at the end of the inner function after the for-loop, otherwise it will get called as soon as the query is called, possibly killing the connection you're trying to use!

clay
  • 5,917
  • 2
  • 23
  • 21
0

perhaps mysql-server is not to be connected, when you query,or mysql-server is to be closed when you query.

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'wm51.wedos.net',
    user     : 'xxxxxx',
    password : 'xxxxxx',
    database: 'd57283_vs'
});

connection.connect();
process.nextTick(function() {
    var queryString = 'SELECT * FROM versus LIMIT 5';

    connection.query(queryString, function(err, rows, fields) {
        connection.end();
        if (err) throw err;
        for (var i in rows) {
            console.log(rows[i].title); 

        }
});

});
Nelson.li
  • 521
  • 4
  • 6
  • doesnt work either. it must be something really stupid, i checked the installs, dynamic dependencies, everything seems alright... – yadayada Feb 02 '14 at 21:27
0

I believe the connection.end() statement still needs to be after the for loop. Or else your ending the connection before the loop even starts.

Meccles
  • 11
  • 3