[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-db
Subject:    [PHP-DB] dynamic sql query
From:       Beth Tucker <beth () phparch ! com>
Date:       2008-07-07 14:53:27
Message-ID: 48722DE7.9040007 () phparch ! com
[Download RAW message or body]

Dear Nasreen,
   You could also do something like this:

$query = "select * FROM gig g, venue v, genre ge ";

if($gig_name) {
  $wheres[] = "g.gigName LIKE '%".$gig_name."%'";
}
if($gig_date) {
  $wheres[] = "g.gig_date LIKE '%".$sdate."%'";
}

if(is_array($wheres)) {
   $wheres = implode(" OR ", $wheres);
   $query .= " WHERE $wheres";
}

This has fewer if statements than basing it off of the number of fields and then \
checking if a value exists to add the " OR " between the statements. Beth


-- 
Beth Tucker
php|architect Trainer
http://www.phparch.com/phptraining/

Check out the latest issue of 
php|architect Magazine:
http://www.phparch.com/c/phpa/magazine/current/

Join us in Atlanta, GA, November 12 - 14, 2008
Explore the Possibilities at php|works
Call for papers open until July 25th
http://phpworks.mtacon.com/


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic