Stuart Felenstein wrote:
--- "Adams, Pat 006" <[EMAIL PROTECTED]> wrote:
You could add a where condition that's always true to the main part of the SQL statement so that you can just tack on more clauses conditionally.
$sql .= "SELECT PostStart, JobTitle, Industry, LocationState, VendorID " . "FROM VendorJobs" . "WHERE 1 = 1 "; if ($s_Ind) { $sql .= "AND VendorJobs.Industry IN ($s_Ind) "; } if ($s_State) { $sql .= " AND VendorJobs.LocationState IN ($s_State)"; }
See I knew I wasn't crazy as Rhino may have suggested :). I had a vague idea that 1=1 would have worked but sadly I did not test it. Anyway, I went with just a blank array and as values are set , the where clause grows dynamically.
Thanks for your help.
Stuart
Well, you could do that (1=1), but why complicate the query to simplify the code? Of course, the optimizer is smart enough to drop a condition which is always true, but it's usually better to do the reverse: complicate the code to send the correct query. I think the array solution you already have is probably better, but you could also fix your original query with two more ifs:
$sql .= "SELECT PostStart, JobTitle, Industry, LocationState, VendorID FROM VendorJobs";
if ($s_ind OR $s_State) {$sql .= "WHERE ";}
if ($s_Ind) {$sql .= "VendorJobs.Industry IN ($s_Ind)";}
if ($s_ind AND $s_State) {$sql .= " AND ";}
if ($s_State) {$sql .= "VendorJobs.LocationState IN ($s_State)";}
-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]