EZ Select is a simple PHP class for generating SELECT queries. It is intended to
make it easier to create complex queries especially when creating them based on user input.
Copyright (c)2002 Pineapple Technologies (http://www.triviashock.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
----------------------------------------
Email questions, feedback, bug reports, etc. to gplstuff@triviashock.net
This package should come with three example files:
example1.php
example2.php
example3.php
Check out those files for examples on how to use EZ Select.
----------------------------------------
Using EZ Select
----------------------------------------
To use EZ Select, you can either copy and paste the code out of ez_select.php into your
own program/library, or simply include ez_select.php like this:
require("ez_select.php");
The interface is very straightforward and demonstrated in example1.php, example2.php,
and example3.php
----------------------------------------
Creating a new query
----------------------------------------
Simply create a new object, like so:
$my_query = new ez_select();
----------------------------------------
Adding fields to the query
----------------------------------------
Adding fields to select (SELECT field_1, field_2 ...) is done via the add_field() method:
$my_query->add_field("field_1");
$my_query->add_field("my_table2.field_2");
----------------------------------------
Adding tables to select from
----------------------------------------
To add tables to select from (the FROM ... part of the query), use the add_table() method:
$my_query->add_table("my_table1");
$my_query->add_table("my_table2");
You can do LEFT JOIN and such like this:
$my_query->add_table("my_table1 LEFT JOIN my_table2 ON my_table1.id=my_table2.id");
----------------------------------------
Adding where clauses
----------------------------------------
Adding a where clause is done by using the add_where_clause() method
$my_query->add_where_clause("my_table1.id = my_table2.id");
$my_query->add_where_clause("my_table1.name != 'foobar'");
----------------------------------------
Adding group by clauses
----------------------------------------
Use the add_group_by() method. They will appear in the query in the order that you add them.
$my_query->add_group_by("my_table1.id");
----------------------------------------
Adding order by clauses
----------------------------------------
Use the add_order_by() method. They will appear in the query in the order that you add them.
$my_query->add_order_by("my_table1.id");
$my_query->add_order_by("RAND()");
The ORDER BY clause in the generated query would look like: "ORDER BY my_table1.id,RAND()"
----------------------------------------
Setting the order type
----------------------------------------
Setting the order type (Ascending or Descending, i.e. ASC or DESC) is done by the set_order_type()
method:
$my_query->set_order_type("ASC");
----------------------------------------
Setting the limit/offset
----------------------------------------
Use the set_limit() method to add a LIMIT clause to the query:
$my_query->set_limit(0, 25);
The first argument is the offset (what row to start at), and the second is the number of rows to return.
The offset defaults to 0 if you don't specify anything.
----------------------------------------
Outputting the query (debugging)
----------------------------------------
To output the color coded sample query, use the show() method:
$my_query->show();
----------------------------------------
Generating the query
----------------------------------------
To return the final query, use the make() method:
$query = $my_query->make();
$result = mysql_query($query);
OR:
$result = mysql_query( $my_query->make() );
|