RGBWaterBlock
RGBWaterBlock

Reputation: 159

Array in sql statement not working with placeholder ?nodejs

sql query does not accept array as value when it is used in a placeholder, It only returns one result even though result is greater than 1. Not using a placeholder and withouting escaping it works perfectly returns the right amount of results.

//works
SELECT * FROM users WHERE userId IN (" + followerIds.join() + ");";
//does not work
SELECT * FROM users WHERE userId IN (?);";
con.query(queryFollowerstTable, [followeringIsd.join()],function(err,result)..

Upvotes: 0

Views: 1266

Answers (2)

RGBWaterBlock
RGBWaterBlock

Reputation: 159

All I had to do was parse followerIds.join() to an int and It worked.

followerIdsParsed = followerIds.join().split(',').map(Number);
followingIdsParsed = followingIds.join().split(',').map(Number);
var queryFollowerstTable = "SELECT * FROM users WHERE userId IN (?); SELECT * 
FROM users WHERE userId IN (?);";
con.query(queryFollowerstTable, [followerIdsParsed, followingIdsParsed], 
function(err, result) {..

Upvotes: 1

Lea Fox
Lea Fox

Reputation: 76

Change con.query(queryFollowerstTable, [followeringIds.join()],function(err,result) to con.query(queryFollowerstTable, followeringIds.join(),function(err,result)

In your original example: SELECT * FROM users WHERE userId IN (" + followerIds.join() + ");";

You are passing in a string not an array

Upvotes: 0

Related Questions