Syntax 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
mattmcb
Forum Commoner
Posts: 27
Joined: Sun Jan 25, 2004 3:34 pm

Syntax Question

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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';
Last edited by feyd on Wed Jun 23, 2004 1:36 am, edited 2 times in total.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Just for future reference it's called a ternary. Maybe that will help someone find this post in the future.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh.. ternary means 3 things required in this case.. Although it is the only ternary operator.. so.. fine..
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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')
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Well that should help the BB's search function. :D
Post Reply