Re: dynamic query string
| From: | Analysis & Solutions | Date: | Tue, 12 Mar 2002 04:12:45 +0000 |
| Subject: | Re: dynamic query string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-88145@lists.php.net to get a copy of this message | ||
> and I like, if possible, that someone can send me tips in how is the best
> way to concatenate the query string in order to do the search. The users
> will have about 8 types of options to search, age (18 - 25, 25-30..),
> gender, nationality.....
My favorite technique is to stick the WHERE's into an array and then
implode that array into the query string. Sample code is below. A
similar technique can be used for creating JOIN statements as well.
By the way, make sure to clean and validate your data before using it in
queries.
<?php
if ($Form->ThemeID != '') {
switch ("$Form->ThemeIs") {
case 'is':
$Where[] = "ThemeID=$Form->ThemeID";
break;
case 'isnot':
$Where[] = "ThemeID!=$Form->ThemeID";
}
}
$SQL->SQLQueryString = 'SELECT ItemID, Item FROM Items';
if (isset($Where)) {
$SQL->SQLQueryString .= ' WHERE ' . implode(' AND ', $Where);
}
$SQL->SQLQueryString .= ' ORDER BY Item';
?>
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