EasySQL Documentation
Introduction
EasySQL provides the same interface to access MySQL, PostgreSQL and SQLite. It also has friendly
methods that will help to create and execute "SELECT", "INSERT",
"UPDATE", "DELETE" and "TRUNCATE" queries.
How to Use
First, it will be necessary import EasySQL class file to the script. Than you may create the object from
EasySQL class.
1. Creating the EasySQL object:
<?php
require_once "class.easysql.php";
$SQL = new EasySQL("MySQL", "host", "user", "password", "database_name", 3306);
?>
Parameters are: database software (e.g. "MySQL", "PostGreSQL", "SQLite"),
host (default: "localhost"), user, password, database that will be selected and server port. This
last one is optional, and will assume default ports for each server software (3306 for MySQL, 5432 for PostGreSQL).
NOTE: on SQLite, parameters "host", "user", "password"
and "port" will be automatically ignored, because this database won't use them. So, you can use
an alternative method to create the object when using SQLite:
2. SQLite allows a different instance:
<?php
$SQL = new EasySQL("SQLite", "host", "user", "password", "database_name", 3306);
$SQL = new EasySQL("SQLite", "database_name");
?>
And now a real life example. Let's suppose you want to select the "name" and "email" of all
clients that celebrate birthday in this month. The name of database in this example is "my_database" and
the selected table is "clients".
3. Using SELECT clause:
<?php
$SQL = new EasySQL("MySQL", "localhost", "root", "password", "my_database");
$SQL->useTable("clients");
$SQL->setSelection("name, email");
$SQL->setWhere("MONTH(birthday) = MONTH(now())");
$SQL->select();
while ($row = $SQL->fetchArray()) {
echo "<br />" . $row['name'] . " - " . $row['email'];
}
echo "<br />Total: " . $SQL->total_rows;
?>
If your database software were PostGreSQL or SQLite, you would use exactly the same functions.
NOTE: you do NOT need to call connect() or close() methods when using
query(), select(), insert(), update(), delete() or
truncate() methods. They all will test if the object has been connected to the database. If not,
it will create a temporary connection (only during the query execution).
Methods
void EasySQL ( str $db_software, str $host, str $user, str $password, str $db_name [, int $port ] )
Class constructor. Parameters are:
- database_software: could be "mysql" or 0, if using MySQL. "pg","postgre",
"postgresql" or 1, if using PostGreSQL. And "sqlite" or 2 if using SQLite. This
parameter is case insensitive, so "MySQL" is equal to "mysql".
- host: host address (default if parameter is empty: localhost).
- user: database username.
- password: password for username.
- database_name: database that will be used.
- port: port number. This is optional, and will assume 3306 for MySQL, and 5432 for PostGreSQL.
resource connect ( [str $db_name] )
Opens a connection to the specified database. If no parameters were passed, it will try the database defined on
the class constructor.
resource pconnect ( [str $db_name] )
The same of connect(), but the conection opened is persistent.
bool close ()
Closes the connection opened by connect().
bool disconnect ()
This method is an alias to close().
resource query ( str $query )
Sends a query.
array fetchArray ( [resource $result] )
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. This method works like the
PHP native "fetch_array" functions (as mysql_fetch_array(), for example). If no parameters were defined, it
will use the result of the last executed query.
array fetchAssoc ( [resource $result] )
Returns an associative array that corresponds to the fetched row, or FALSE if there are no more rows. This method works
like the PHP native "fetch_assoc" functions (as mysql_fetch_assoc(), for example). If no parameters were defined,
it will use the result of the last executed query.
array fetchRow ( [resource $result] )
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. This method works
like the PHP native "fetch_row" functions (as mysql_fetch_row(), for example). If no parameters were defined, it
will use the result of the last executed query.
void useDatabase ( str $db_name )
Changes the database name specified in the class constructor.
str error ( )
Displays the last error.
Methods below are intended to make easier the process of creating and executing queries. You will not necessarily use them.
void useTable ( str $table_name )
Defines what table should be used when creating and executing the SQL query.
void setFieldValue ( str $field, mixed $value, bool $is_sql_function )
Creates an internal list of field values that will be used on INSERT or UPDATE queries. Optional parameter sets if
the parameter passed on $value should be interpreted as an SQL function, like NOW(), CONCAT(), etc.
void setWhere ( str $where_clause )
Defines the WHERE clause used on SELECT, UPDATE and DELETE queries. Use this method without writing "WHERE" in
the beginning of the parameter. The right way is: setWhere("id > 10 AND name LIKE '%Reche%'");
void setLimit ( str $limit_clause )
Defines the LIMIT clause used on SELECT, UPDATE and DELETE queries. Use this method without writing "LIMIT" in
the beginning of the parameter. The right way is: setLimit("0, 20");
void setSelection ( str $selection )
Defines the selected fields on SELECT clause. Default is "*"
resource select ( )
Executes a SELECT query.
resource insert ( )
Executes an INSERT query.
resource update ( )
Executes an UPDATE query. If where clause is not defined, the query execution is aborted for security reasons.
resource delete ( )
Executes a DELETE query. If where clause is not defined, the query execution is aborted for security reasons.
resource truncate ( )
Executes a TRUNCATE query (erases all contents of the table).
Credits
Carlos Reche
carlosreche at yahoo dot com
Jan 5, 2005
Sorocaba, SP - Brazil