Jump to content

conditional select statement


steveginer

Recommended Posts

Hi,
Is there any way you can make your WHERE clause conditional upon which variables are passed from the referring form?  Can one embed php tags within a query, if not what is the general approach to a problem like this?

It works OK in coldfusion but can't seem to figure out how to recreate this in php. Thanks. Here's the coldfusion code.

<cfquery name="xxxx" datasource="xxxx" username="xxxx" password="xxxx">
SELECT DISTINCT style.style, style.desc, style.group
FROM style
WHERE 0=0
<cfif URL.style IS NOT "">
AND style.style = '#URL.style#'
</cfif>
<cfif URL.desc IS NOT "">
AND style.desc like '#URL.desc#'
</cfif>
<cfif URL.group IS NOT "">
AND style.group = '#URL.group#'
</cfif>
<cfif URL.OTS IS NOT "">
AND stktot-ordtot+wiptot>#URL.OTS#</cfif>
<cfif URL.IMMED IS NOT "">
AND stktot-ordtot>#URL.IMMED#</cfif>
<cfif URL.makebuy IS NOT "">
AND style.makebuy = '#URL.makebuy#'
</cfif>
</cfquery>
Link to comment
https://forums.phpfreaks.com/topic/25733-conditional-select-statement/
Share on other sites

Is the period after $Query correct syntax?...meaning $Query . or is it the semicolon that tells php that the query is being continued or built on the fly...still not clear

$Query = 'SELECT * FROM shirts';
if($wantsRedItems )
$Query .= ' WHERE color=red';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.