Re: PHP Problem: If else statement

by "Michael Gerholdt" <gerholdt(at)fredonia.edu>

 Date:  Thu, 13 Jun 2002 14:16:36 -0400
 To:  <richard(at)rkwinternet.com>,
"HWG" <hwg-languages(at)hwg.org>
 References:  rkwinternet
  todo: View Thread, Original
>
> ==============================
>
>   if ($bedrooms = 'NULL') {
>
>       $result = mysql_query("SELECT advert, property, price, description,
bedrooms, price_id, P_No FROM adverts WHERE price_id =
> '$price_id'"); }
>
>   else {
>
>   $result = mysql_query("SELECT advert, property, price, description,
bedrooms, price_id, P_No FROM adverts WHERE price_id =
> '$price_id' AND bedrooms = '$bedrooms'"); }
>
> ===============================

Simplify a bit:

If (isNull($bedrooms) {
   $bedrooms = '%';
}

$strSQL  = "SELECT advert, property, price, description, bedrooms, price_id,
P_No ";
$strSQL .= "FROM adverts ";
$strSQL .=  "WHERE price_id ='$price_id' AND bedrooms = '$bedrooms'";

#uncomment for debugging
#echo $strSQL

$result = mysql_query($strSQL)

 ===============================

I don't have my PHP functions with me (mostly do VBScript and ASP) so you
might have to swap out the function call "isNull" for the correct PHP
version

This way, at any rate, you don't have to write multiple sql statements - its
a bit easier to read/debug etc. You can also uncomment the echo statement,
see the SQL string that's being built, and run it against your database
directly to see what errors or results you get.

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