Re: Dynamic Queries

From: Date: Mon, 27 Nov 2000 23:57:45 +0000
Subject: Re: Dynamic Queries
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-27508@lists.php.net to get a copy of this message
Well I would put your search by's in an array like this: <INPUT TYPE="text" NAME="search[fname]"> <INPUT TYPE="text" NAME="search[lname]"> <INPUT TYPE="text" NAME="search[zip]"> Then do this: while(list($key,$val) = each($search)){ $final_sql[$key] = $$key.'=\''.$val.'\''; } $sql = "SELECT * FROM accounts WHERE ".implode(' && ',$final_sql); You can then add your order by if you want like this: $sql .= " ORDER BY ".$order_by; Where $order_by was a select box (for example) --Joe On Mon, Nov 27, 2000 at 06:18:07PM -0500, 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). > > Obviously, this is NOT the way to do it, but it'll give you an idea of > what I'm up to: > > $str_sql = " > select * > from pers_list2 > WHERE > if (isset($fname)) { > fname = '$fname' > } > if (isset($fname) && isset($lname)) { > AND > } > if (isset($lname)) { > lname = '$lname' > } > order by lname > "; > > Thanks very much, > Jamie > > -- > 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 ================================================================================ = Joe Stump joestump98@yahoo.com http://www.miester.org = ================================================================================ Need a programmer? http://www.google.com/search?q=joe+stump+resume

« previous php.general (#27508) next »