Re: Dynamic Queries
| From: | Uioreanu Calin | Date: | Tue, 28 Nov 2000 14:32:45 +0000 |
| Subject: | Re: Dynamic Queries | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-27598@lists.php.net to get a copy of this message | ||
Hello,
Something like:
$Arr["fname"]=$fname
$Arr...
...
reset($Arr);
unset($Str_Cond);
while ( list($k, $v) = each($Arr)
{
if ($v)
$Str_Cond .= "AND $k='$v'";
}
$Str_Cond = substr($Str_Cond,3); // chop first AND
$SQL_Query = "SELECT ... $Str_Cond";
...
This way you need to initialise an array and a few variables from other
postings.
Regards,
------------------------------------------------------
Calin Uioreanu
udmsromkt@mail.dntcj.ro
DMS Team Cluj-Napoca Romania
-----------------------------------------------------
"Jamie Jackson" <NOSPAMjacksonjNOSPAM@calib.com> wrote in message
news:kgq52t8tdeui3cil9fkgauimttci7neo2h@4ax.com...
> 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
>