Page 1 of 1

Syntax Question

Posted: Tue Jun 22, 2004 1:15 pm
by mattmcb
What is the proper syntax to include and if statement while trying to write out a query? Basically, this value might be in the DB... if it is it needs to be in the query with a comma but if it's not it shouldn't be in the query with a comma.

The IN statement of the query is where my question is directed at:

Code: Select all

$sql2 = 'SELECT username'
. ' FROM sgforum_users'
. ' WHERE user_id'
. ' IN ( '.$row1['user_id_1'].''.if (isset($row1['user_id_2'])) echo (','.$row1['user_id_2']).''.if (isset($row1['user_id_3'])) echo (','.$row1['user_id_3']).''.if (isset($row1['user_id_4'])) echo (','.$row1['user_id_4']).' ) LIMIT 0, 30';

echo $sql2;

Posted: Tue Jun 22, 2004 1:32 pm
by feyd
?: operator..

example:

Code: Select all

$sql = 'SELECT `username` FROM `sgforum_users` WHERE `user_id` IN('.$row1['user_id_1'].(isset($row1['user_id_2'])?','.$row1['user_id_2']:'').(isset($row1['user_id_3'])?','.$row1['user_id_3']:'').(isset($row1['user_id_4'])?','.$row1['user_id_4']:'').') LIMIT 0, 30';

Posted: Wed Jun 23, 2004 10:20 am
by Buddha443556
Just for future reference it's called a ternary. Maybe that will help someone find this post in the future.

Posted: Wed Jun 23, 2004 11:25 am
by feyd
uh.. ternary means 3 things required in this case.. Although it is the only ternary operator.. so.. fine..

Posted: Wed Jun 23, 2004 2:29 pm
by Weirdan
feyd wrote:uh.. ternary means 3 things required in this case..
do you know another ternary operator? In C, PHP and other languages I used the only ternary operator is ?: (and it's always called 'ternary operator')

Posted: Wed Jun 23, 2004 2:42 pm
by Buddha443556
Well that should help the BB's search function. :D