THE ZEPINT NETWORK

programmer assist

PHP PHP XML Feeds

PHP Questions PHP Solutions PHP Articles

PHP is a popular open-source programming language used primarily for developing server-side applications and dynamic web content, and more recently, other software. The name is a recursive acronym for "PHP: Hypertext Preprocessor".

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/

Got a PHP Question?

Just Sign Up and ask the top PHP experts!

Search via Google

User Login

Email Address

Password

PHP Experts

Rank Expert Points
#1 Srirangan 2650
#2 Anurag 600
#3 mastercomputers 100
This a list of the Top PHP experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?