Home arrow MySQL arrow Page 4 - PHP and Databases for the Lazy Sod

PHP and Databases for the Lazy Sod

In this article Justin explains his ezSQL utility, which reduces code and development time for database operations in PHP. This is great for the lazy sod...and everyone else.

Author Info:
By: Justin Vincent
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
January 19, 2003
  1. · PHP and Databases for the Lazy Sod
  2. · Atomic Operations
  3. · Use PHP Functions not DB Functions!
  4. · Abstraction
  5. · Getting Even Lazier
  6. · Conclusion

print this article

PHP and Databases for the Lazy Sod - Abstraction
(Page 4 of 6 )

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:


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 only database specific functions are $db->query, $db->select and $db->db (the constructor function). All other functions use standard PHP code only.

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") )
  echo "You are logged in!";
  echo "Sorry. Bad user name or bad password.";

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:

if ( $pwd == $db->get_var("SELECT pwd, name, id FROM users WHERE id = 2") )
  $user = $db->get_row(null);

  echo "Hello $user->name your ID is $user->id and you are now logged in!";
  echo "Sorry. Bad user name or bad password.";

Neat! There are a number of reasons why this works.
  1. 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).
  2. 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.
  3. 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.

blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials