lundi 28 mai 2018

Pick a number check a db if its there repeat if not store

I am trying to do some code where my server picks a random number between 1 and 5. Then that is done it checks a MySQL DB to see if it is already included in the DB. If it is in the DB it repeats the process. If the number is not found then it adds it to the DB.

I am trying to write the code to do that, however, I am having some difficulty. My attempt is below

let result = 0;
con.connect(function(err) {
if (err) throw err;
while (result >= 1)
    {
        let x = Math.floor((Math.random()*5)+1);
        con.query("SELECT column1, column2 FROM users where column2 = x", function (err, result, fields) 
        {
            if (err) throw err;
            result = result.length;
        });
    }
var sql = "INSERT INTO users (column1, column2) VALUES ('a string', x)";

});




Aucun commentaire:

Enregistrer un commentaire