PHP and Working with Databases (for the Lazy Sod)

By HTMLGoodies Staff


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.

Page 5: Getting Even Lazier

Page 4 of 6

Previous Page
1 2 3 4 5 6
Next Page

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date