Abstraction
It's not hard to see how this makes it very easy to port your code from one database to another (as long as you use ANSI SQL in your database queries). In the ezSQL class itself there are only three functions that are database specific.
I have already ported the class from mySQL to Oracle8. It took me about 30 minutes to change the three functions that needed changing. This means that I can run exactly the same script using either an Oracle8 database or a mySQL database. All I have to do is to replace the include file ez_sql.php.
A more obvious example might be:
- <?php
- if ( $server == "oracle8" )
- include_once "oracle8/ez_sql.php";
- include_once "mySQL/ez_sql.php";
$users = $db->get_results("SELECT * FROM users");
foreach ( $users as $user )
{
- echo $user->name;
Functions You Might Need
When I wrote ezSQL, the atomic functions I included were:
- $db->get_results - Get multiple row result set from the database
$db->get_row - Get one row from the database
$db->get_col - Get one column from query based on column offset
$db->get_var - Get one variable, from one row, from the database
$db->query - Send a query to the database (and if any results, cache them)
$db->debug - Print last SQL query and returned results (if any)
$db->vardump - Print the contents and structure of any variable
$db->select - Select a new database to work with
$db->get_col_info - Get information about columns such as column name
$db = new db - Initiate new db object
The other point to note is that the two main functions that return rows of results take an optional argument to specify whether to return the results as an associative array, numerical array or object (which is the default). So, now let's have a look at a few more useful ways to work with this new class.
Say I want to validate a user's password against a password stored in the database.
I could do this:
if ( $pwd == $db->get_var("SELECT pwd FROM users WHERE id = 2") )
But wait, we can go one better. This time we can check the password while at the same time pulling in extra user details that we can use if the password is valid:
{
echo "You are logged in!";
}
else
{
echo "Sorry. Bad user name or bad password.";
}
if ( $pwd == $db->get_var("SELECT pwd, name, id FROM users WHERE id = 2") )
Neat! There are a number of reasons why this works.
{
$user = $db->get_row(null);
}
echo "Hello $user->name your ID is $user->id and you are now logged in!";
else
{
echo "Sorry. Bad user name or bad password.";
}
The function $db->get_var() always returns the variable that is stored in the first column of the first row of the results (unless otherwise specified). Even though we have only extracted one variable using $db->get_var(), the query itself asked for three columns of information. The full results have been cached as a query result set within the db object, ready for any other ezSQL function to use. We have taken full advantage of this caching technique by using the function $db->get_row with a null query. This executes the main code of the function, but instead of getting the results from the database it gets the results from the previously cached result set.
