Interacting with databases - The ezSQL Way
DiggBlinkRedditDeliciousTechnorati
article by Srirangan
ezSQL is a PHP class created by Justin Vincent (justinvincent.com). ezSQL makes it very fast and easy for you to use database(s) within your PHP scripts ( mySQL / Oracle8/9 / InterBase/FireBird / PostgreSQL / MS-SQL / SQLite / SQLite c++).
My Experience
I have used ezSQL for the past 3 years of my web development and have found it extremely useful, especially when it comes to improving productivity and saving time.
Features
- It is one php file that you include at the top of your script. Then, instead of using standard php database functions listed in the php manual, you use a much smaller (and easier) set of ezSQL object functions.
- It automatically caches query results and allows you to use easy to understand functions to manipulate and extract them without causing extra server overhead
- It has excellent debug functions making it lightning-fast to see what?s going on in your SQL code
- Most ezSQL functions can return results as Objects, Associative Arrays, or Numerical Arrays
- It can dramatically decrease development time and in most cases will streamline your code and make things run faster as well as making it very easy to debug and optimise your database queries.
- It is a small class and will not add very much overhead to your website.
Example 1
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
Example 2
// Get one row from the database and print it out..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;
Example 3
// Get one variable from the database and print it out..
$var = $db->get_var("SELECT count(*) FROM users");
echo $var;
Example 4
// Insert into the database
$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");
Example 5
// Update the database
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");
Example 6
// Display last query and all associated results
$db->debug();
Example 7
// Display the structure and contents of any result(s) .. or any variable
$results = $db->get_results("SELECT name, email FROM users");
$db->vardump($results);
Example 8
// Get 'one column' (based on column index) and print it out..
$names = $db->get_col("SELECT name,email FROM users",0)
foreach ( $names as $name )
{
echo $name;
}
Example 9
// Same as above ?but quicker?
foreach ( $db->get_col("SELECT name,email FROM users",0) as $name )
{
echo $name;
}
Example 10
// Map out the full schema of any given database and print it out..
$db->select("my_database");
foreach ( $db->get_col("SHOW TABLES",0) as $table_name )
{
$db->debug();
$db->get_results("DESC $table_name");
}
$db->debug();
Download
The class can be downloaded from http://justinvincent.com/