Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PHP/MySQL Query Database with Variable number of fields



--- On Wed, 11/3/10, Myrle Francis <mafmanet-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
> I Know how to query any one field or multiple fields but
> want I want to do
> is have field1, Field2 and Field3 populated in a drop-down
> list and query my
> database given the number of fields that are selected..
> (maybe just query
> Field1 or query field1 and Field3)
[snip]

Forgive me if the syntax is hair off, but how about something like:

$fields_arr = array();
if (isset($field1)) $fields_arr[] = $field1;
...
if (isset($fieldn)) $fields_arr[] = $fieldn;

$sql = "select foo from bar where ";
$sql .= implode(" and ", $fields_arr);
$sql .= " more sql here;";

-Dan



      






BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org