Re: Newbie: PHP/MySQL (SELECT)

From: Date: Fri, 22 Nov 2002 14:53:44 +0000
Subject: Re: Newbie: PHP/MySQL (SELECT)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-125615@lists.php.net to get a copy of this message
if one of $titulotxt or $cdstxt is not set, your query will look something like this: SELECT * FROM divx WHERE && cds like '$cdstxt' ORDER BY titulo As you see, there is unnecessery &&. I build my search queries using this form: $query_cond=''; foreach($_GET as $col => $val) { switch($col) {
       case 'cds': // you can add more cases here for conditions that need to be exact
           $query_cond .= " $col LIKE '$val' AND ";
           break;
       case 'titulo': // you can add more cases here for conditions that need not to be exact
           $query_cond .= " $col LIKE '%$val%' AND ";
           break;
} } // get rid of final AND $query_cond = substr($query_cond, 0, strlen($query_cond) - 4); // and as you don't have any other conditions, $query_cond cannot be empty - we would have excessive WHERE // so if it is empty, make it 1 if($query_cond=='') $query_cond='1'; $sql="SELECT * FROM divx WHERE $query_cond ORDER BY titulo"; Mr. BuNgL3 wrote:
Hi... I'm with a little sintax problem... The question is that i have two search fields (titulotxt and cdstxt) and i want to create an mysql condition... i trying: $sql1=($titulotxt) ? "titulo like '%".$titulotxt."%'":""; $sql2=($cdstxt) ? "cds like '$cdstxt'":""; $sql="SELECT * FROM divx WHERE" .$sql1 " && " .$sql2 " ORDER BY titulo"; but he's giving me a sintax error on the 3 line... Can anyone teach me how i must do to validate the mysql condition and make it work? Thanks


« previous php.general (#125615) next »