MySQL Forums
Forum List  »  PHP

Re: Problem with search when add BETWEEN to query
Posted by: Rick James
Date: April 23, 2010 10:01AM

Try out this pattern for size:

$wheres = array();   // For gathering the things to AND together

// Take care of all 4 cases with 0, 1, or 2 where clauses:
if (! is_number($min_price))  die("bad price");
if (! is_number($max_price))  die("bad price");
if isset($min_price)    $wheres[] = "price >= $min_price";
if isset($max_price)    $wheres[] = "price <= $max_price";

if (isset($type)) $wheres[] = "(type LIKE '".mysql_real_escape_string($type)."')";
// etc...

if (count($wheres) == 0)  die("Hey! You gotta enter something!");
// Add in the ANDs:
$where_clause = implode(' AND ', $wheres);

Then build the rest of the SELECT, tacking on $where_clause .

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.