Reply
Old 06-22-2004, 03:18 PM Syntax Question
Novice Talker

Posts: 9
Trades: 0
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:

PHP Code:
$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
mattmcb is offline
Reply With Quote
View Public Profile Visit mattmcb's homepage!
 
 
When You Register, These Ads Go Away!
Old 04-25-2008, 12:51 PM Re: Syntax Question
Experienced Talker

Posts: 46
Trades: 0
$sql2 = 'SELECT username FROM sgforum_users WHERE user_id IN';

if (isset($row1['user_id_2']))
{
$sql2 .= $row1['user_id_2'];
} else {
// something else
}

// execute your query
__________________
Squirrel bombing -> Home of the crasher squirrel
jeff_oneil is offline
Reply With Quote
View Public Profile Visit jeff_oneil's homepage!
 
Old 04-25-2008, 01:30 PM Re: Syntax Question
addonchat's Avatar
Super Talker

Posts: 113
Name: Chris Duerr
Trades: 0
I'd try the ternary operator.
E.g.,

PHP Code:
isset($row1['user_id_2']) ? ", $row1['user_id_2']" "" 
http://us3.php.net/language.operators.comparison

In this case though, I'd just use a foreach to iterate through user_id_?, creating an array, then use implode to glue it together for the query.

__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 04-26-2008, 11:45 AM Re: Syntax Question
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Trades: 1
id go with

PHP Code:
if (isset($row1['user_id_1'])) $inVars[]=$row1['user_id_1'];
if (isset(
$row1['user_id_2'])) $inVars[]=$row1['user_id_2'];
if (isset(
$row1['user_id_3'])) $inVars[]=$row1['user_id_3'];
if (isset(
$row1['user_id_4'])) $inVars[]=$row1['user_id_4'];
if (isset(
$row1['user_id_5'])) $inVars[]=$row1['user_id_5'];

$sql2 'SELECT username' 
' FROM sgforum_users' 
' WHERE user_id' 
' IN ( '.implode(',',$inVars).' ) LIMIT 0, 30'
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Reply     « Reply to Syntax Question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.12437 seconds with 13 queries