mysql - Error executing UPDATE -
i'm having little trouble performing update query node mysql2
module. i'm preparing query using '?' placeholder , passing in values so;
socket.on('connection', function(client){ [...] client.on('userjoin', function(username, userid){ run_db_insert("update users_table set clientid = ? user = ?", [client.id, userid], function(){ console.log(client.id + ' <=> ' + userid); }); [...] });
unfortunately, raising error; you have error in sql syntax; check manual corresponds mysql server version right syntax use near ''12345678' userid = ?' @ line 1
the data isn't reflected in database. reason, code doesn't appear picking second question mark placeholder , it's not passing correct value (i.e. it's trying find userid
of ?
).
if change code this;
run_db_insert("update users_table set clientid = ? user = '" + userid + "'", [client.id], function(){
...then update runs without error , reflected in db. if console.log
both client.id
, userid
, console correctly reflects these values.
my run_db_insert
function follows;
function run_db_insert(sql, args, callback){ var mysql = svc_mysql2.createconnection({ // connection details }); mysql.connect(function(err){ if(err){ console.log('error connecting db: ' + err); } }); mysql.query(sql, [args], function(err){ if (err){ console.log(err); return; } callback(); }); mysql.end(); };
i've had no problems performing select or insert queries using multiple '?' placeholders (with modified function has result
in line 11 of function , returns in callback), i'm finding update isn't correctly assigning parameters i'm passing in it.
i think problem you're wrapping query replacement values in array, [[client.id, userid]]
being passed mysql.query()
.
try changing:
mysql.query(sql, [args], function(err){
to:
mysql.query(sql, args, function(err){
Comments
Post a Comment