Re: Need help with PHP & MySQL

by "Darrell King" <darrell(at)webctr.com>

 Date:  Fri, 3 May 2002 18:50:42 -0400
 To:  <hwg-techniques(at)hwg.org>
 References:  D1C4FN01 ntlworld pieceoshit
  todo: View Thread, Original

if (isset($alpha)&&!empty($alpha)) $alpha_clause=" AND Alpha='$ alpha' ";
else $alpha_clause='';

$result=mysql_query("SELECT * FROM
Restaurants WHERE County='$county' $alpha_clause AND Cuisine='$cuisine'
AND Price='$price' ORDER BY Name");

...etc with the other where clause elements.

I didn't test this particular code, obviously, but I have used this
technique successfully.
D


----- Original Message -----
From: "Lisa Bradshaw" <zibbler(at)web-design-cs.com>
To: <hwg-techniques(at)hwg.org>
Sent: Friday, May 03, 2002 6:10 PM
Subject: Need help with PHP & MySQL


Any PHP & MySQL gurus out there? I'm pulling my hair out trying to figure
out how to get this to work. I have a form that someone can use to query a
database of restaurants. The form has four select lists, one for county, one
for cuisine, one for alpha and one for price. Each one has the option to be
left blank. So for instance if I was looking for a seafood restaurant in
Dade county, but didn't enter a selection for alpha or price, in theory it
should return records for ALL seafood reataurants in Dade county regardless
of alpha or price. Following is the code I'm using to query the database:

$result=mysql_query("SELECT * FROM
Restaurants WHERE County='$county' AND Alpha='$alpha' AND Cuisine='$cuisine'
AND Price='$price' ORDER BY Name");

How can I get it to ignore the blank variables and pass the info? Is this
even possible? I tried doing this leaving the county blank but selecting
options from each of the other selections and it returned no records, even
though I knew there were records in there. It's seeing $county as "", and
because there is no "" listed in the database for County, it of course
returns no records. Any way around this? This is my first attempt at using
PHP and MySQL.HELP!!!

TIA,
Lisa

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA