[nycphp-talk] SQL 'WHERE' conditions in an array
Douglas Clifton
dwclifton at gmail.com
Thu Aug 4 13:36:56 EDT 2005
$q->table = 'table_name';
$q->columns = array('column1', 'column2', 'column3');
print_r($q);
function query($q) {
$query = 'select ';
$query .= ($q->columns) ? implode(',', $q->columns) : '*';
$query .= ' from ' . $q->table;
return $query;
}
print query($q) . "\n";
unset($q->columns);
print_r($q);
print query($q) . "\n";
--------------------------
stdClass Object
(
[table] => table_name
[columns] => Array
(
[0] => column1
[1] => column2
[2] => column3
)
)
select column1,column2,column3 from table_name
stdClass Object
(
[table] => table_name
)
select * from table_name
--
Douglas Clifton
dwclifton at gmail.com
http://loadaveragezero.com/
http://loadaveragezero.com/drx/rss/recent
> ---------- Forwarded message ----------
> From: "Kenneth Downs" <ken at secdat.com>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Date: Wed, 3 Aug 2005 12:18:07 -0400 (EDT)
> Subject: Re: [nycphp-talk] SQL 'WHERE' conditions in an array
> You may want to look up "binary decision trees". This is not precisely
> what you are looking for but it is closely related and may give you some
> background.
>
> The basic idea behind any generated SQL statement is the fact that SQL
> statements that follow certain rules can themselves can be stored entirely
> in scalar data.
>
> Imagine this simple array:
>
> $Query = Array
> (
> "tables"=> Array
> (
> [0] = Array
> (
> "table_name"=>"customers"
> )
> )
> )
>
> or the more detailed:
>
> $Query = Array
> (
> "tables"=> Array
> (
> [0] = Array
> (
> "table_name"=>"customers"
> "columns"=> Array
> (
> "column1",
> "column2",
> "column3"
> )
> )
> )
> )
>
> Your code just loops through building a SELECT clause from the tables. If
> it cannot find the "columns" sub-array it puts in "*", else it lists the
> columns in that array.
>
> Everything else is just an embellishment of this, and of course remember
> TIMTOWTDI.
>
>
> > Hello,
> > A while ago I've seen some code (function) to build SQL queries. It took a
> > multidimentional associative array to build complex WHERE conditions.
> > That's
> > the feature that I really liked. I'm trying to find it again to no avail.
> > Can someone point me to that code please?
> >
> > Thanks,
> > Olaf
> > _______________________________________________
> > New York PHP Talk Mailing List
> > AMP Technology
> > Supporting Apache, MySQL and PHP
> > http://lists.nyphp.org/mailman/listinfo/talk
> > http://www.nyphp.org
More information about the talk
mailing list