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
mattmcb
Forum Commoner
Posts: 27 Joined: Sun Jan 25, 2004 3:34 pm
Post
by mattmcb » Tue Jun 22, 2004 1:15 pm
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;
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jun 22, 2004 1:32 pm
?: 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';
Last edited by
feyd on Wed Jun 23, 2004 1:36 am, edited 2 times in total.
Buddha443556
Forum Regular
Posts: 873 Joined: Fri Mar 19, 2004 1:51 pm
Post
by Buddha443556 » Wed Jun 23, 2004 10:20 am
Just for future reference it's called a ternary. Maybe that will help someone find this post in the future.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 23, 2004 11:25 am
uh.. ternary means 3 things required in this case.. Although it is the only ternary operator.. so.. fine..
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Jun 23, 2004 2:29 pm
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 ')
Buddha443556
Forum Regular
Posts: 873 Joined: Fri Mar 19, 2004 1:51 pm
Post
by Buddha443556 » Wed Jun 23, 2004 2:42 pm
Well that should help the BB's search function.