Re: Dynamic Queries

From: Date: Tue, 28 Nov 2000 00:05:51 +0000
Subject: Re: Dynamic Queries
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-27522@lists.php.net to get a copy of this message
Jamie Jackson wrote: > > Hello, > > Newbie question. What's the best, most elegant way to write dynamic > queries? Keep in mind my background is in ColdFusion (and I'm using > PHP3 and MySQL). One possibility: unset($Where); if (isset($fname)) { $Where[] = "fname = '$fname'"; } if (isset($lname)) { $Where[] = "lname = '$lname'"; } $str_sql = "select * from pers_list2"; if ( isset($Where) ) { $str_sql .= " WHERE "; $str_sql .= implode(" AND", $Where); } $str_sql .= " order by lname"; Enjoy, --Dan -- PHP scripts that make your job easier http://www.analysisandsolutions.com/code/ SQL Solution | Layout Solution | Form Solution T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Ave, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409

« previous php.general (#27522) next »