WHERE column IN (...) question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

WHERE column IN (...) question.

Post by s.dot »

when using SQL like WHERE column IN (list here), does it only support integers in the list?

For example, can I do a query like

Code: Select all

$result = mysql_query("SELECT column FROM users WHERE username IN (username1,username2,username3)");
I have tried but it tells me unknown column username1 in where claus
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strings require quoting.....
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

That makes sense. The error is gone, but there's no result now.

Code: Select all

$namelist = implode(",",$names);
$online = mysql_query("SELECT id FROM users WHERE username IN ('$namelist')") or die(mysql_error());
I've echo'd both, and everything appears to be correct. mysql_num_rows($online) returns 0
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$namelist = implode("','",$names);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

That's interesting.

Unless I have each name separated with ',' it treats the whole string as one long name?

Out of curiosity, why doesn't it do this with integers? Because they don't require quotes?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bingo.
Post Reply