PHP/MySQL Query Database with Variable number of fields

Dan Kressin dkressin-/E1597aS9LQAvxtiuMwx3w at public.gmane.org
Wed Nov 3 12:52:07 EDT 2010


--- 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



      





More information about the Discuss mailing list