Re: Any good way ?

From: Date: Fri, 19 Jan 2001 22:41:15 +0000
Subject: Re: Any good way ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-35745@lists.php.net to get a copy of this message
You want to drop empty where clauses? This will work, although your syntax will be wrong if $em is empty ( you will get an SQL syntax error with the extra AND on the end ). You need a dummy clause after the where clause to make things match up right, something that always returns true like 1=1. $query = "select * from TABLE where 1=1 "; if (empty($f)) $query .= " AND FIRST_NAME like '%$f%' "; if (empty($l)) $query .= " AND LAST_NAME like '%$l%' "; if (empty($em)) $query .= " AND ((HOME_EMAIL1 like '%$em%') OR (HOME_EMAIL2 like '%$em%') OR (WORK_EMAIL1 like '%$em%') OR (WORK_EMAIL2 like '%$em%')) "; // put anything else here $query .= " group by COLNAME, order by COLNAME "; You might also want to look into fulltext searches to utilize an index in the database (is this MySQL?) instead of table scans. TV Karthick Kumar wrote: > > Hi all > > I have written the following code, but I dont' think it's a good way to > write like this. Is there some other way to do good programming for this ?! > > **************************************************************************** > ****************** > if ($f) > { > $SQL .= " (FIRST_NAME like '%$f%') AND "; > } > if ($l) > { > $SQL .= " (LAST_NAME like '%$l%') AND "; > } > if ($em) > { > $SQL .= " (HOME_EMAIL1 like '%$em%') OR (HOME_EMAIL2 like '%$em%') > OR > (WORK_EMAIL1 like '%$em%') OR (WORK_EMAIL2 like '%$em%') "; > } > **************************************************************************** > ****************** > > Any help is appreciated very much. > > Thanks in adv. > > ~ Karthick > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net -- Monte Ohrt <monte@ispi.net> http://www.ispi.net/

« previous php.general (#35745) next »