RE: Absolute Beginners – PHP 101 (part 8): Databases and Other Animals

A very good friend of my is a web designer who has always struggled with programming. He found the Absolute Beginners – PHP 101 series of articles from Zend to be extremely helpful. I noticed he is learning things better this time simply by the types of questions he asks me. He started asking me some database things and my immediate response was “don’t do it that way”. My response was of course wrong. After all, he was just learning. The 8th article in the series introduces database access and honestly it feels a bit out of place after the first 7 articles. The first 7 take an almost programming 101 (php101 right?) approach and give good fundamentals. With a little bit more background in each article, the series could almost be the first few weeks of an introductory college level computer science (programming) course. The next steps would be to introduce data abstraction, common abstract data types, and algorithms, but Zend knows their audience here. These are people who just want to get things done.

http://www.zend.com/php/beginners/php101-8.php is the article to which I am refering. It is fine and there is nothing wrong with it, but it does lead the new programmer down a dangerous path. There is no mention of abstraction.

Using an abstract library instead of direct database function calls means that if you want to switch databases from MySQL to PostgreSQL in the future, it will be a lot easier. Here is what the example code might look like.

Instead of calling the mysql_ suite of functions directly, PHP provides an excellent Database Abstraction library in the PEAR library known as PEAR::DB. I used PEAR::DB back in 2001 on one medium sized web project and it worked great. I had used it prior for many months without issue. I’m sure it has matured over the past 5 years.

I had initially written an article directly converting the examples in the Absolute Beginners – PHP 101 article 8 to using the PEAR libraries. Due to a horrible typo, I lost an hour of work and I never revisited this task. Instead of write a side by side comparison of direct mysql_ function calls to PEAR::DB connection instance OO calls, I’ll simply reference a PEAR:DB tutorial article. A great article is available at evolt called Abstract PHP’s database code with PEAR::DB. A huge list of resource is available from PHP Kitchen. International PHP magazine has an Introduction to PEAR which eases the reader into PEAR::DB.

So before calling a mysql_, pg_, ibase_, fbsql_, db2_, maxdb_, mssql_, sqlite_ or some other database function, consider using PEAR::DB, so that you don’t have to guess if s/mysql_/pg_/ will work or not. Of course, writing cross database SQL is something else entirely, but this is a necessary first step.

1 thought on “RE: Absolute Beginners – PHP 101 (part 8): Databases and Other Animals”

  1. ummmmm… i’m afraid your “friend” appreciates it. i’m sure your “friend” is also super awesome at stuff and totally has a big wang.

Comments are closed.