There are many ways a user can work with a database. The user may use the native functions available through php for the particular database, the PEAR DB package, the ADOdb package, or the user can create a custom database package. Each of these options have good and bad points but we will be discussing and comparing the native functions for a database and the PEAR DB ackage.
In my examples I will pretend to be using a MySQL database. You can find the native functions for it here in the manual. If you are using a different database system simply go to the user manual and look for your system - there you will find it's listing of available functions. If you still can't find your database system - look at the list of systems supported by the ODBC drivers. I will not be using all of the available functions for MySQL.
You can find the documentation for the PEAR DB here to look up what I am discussing or for your future reference.
In this section of the article we will compare/demonstrate native database functions vs. PEAR DB. First we will quickly go over some of the advantages/disadvantages of the 2, then how to connect to the database with each, how to send a query and retreive the results, and lastly how to free the results and close the connection.
Please notice that I will be including code to handle possible errors that may occur. You should NEVER assume that what should happen always will. All the error code will be commented so you follow what it is doing. Murphy (aka Murphy's Law) tends to pop up when ever and where ever he chooses, so make sure you are ready for him.
Advantages
The biggest advantage of native functions is speed. Nothing else compares. The next major advantage is simply that the functions available for a database - maximize the database's effectiveness. Native functions for a database system takes advantage of all the systems capabilities.
The biggest advantage of PEAR DB is its portability. This package can work with MySQL, PostgreSQL, MSSQL, and a host of others. The only thing that will have to be changed is 1 to 2 lines to switch between database systems. The next major advantage is that it is Object Oriented. With the the new Object Oriented PHP 5 this is a definate advantage.
Disadvantages
Native functions are database specific. If you change from a MySQL database to a PostgreSQL database - all your function names will have to be changed. And some function calls that were available in one database system may not be available in another. Also if you are programming with objects - depending on how you program it - you may have to create a wrapper class for the database.
PEAR DB is slow. Granted there are ways to speed it up but it still will not be as fast as the native functions. Also PEAR DB has a limited number of method calls - so you may have to make 3 rights to go left. Lastly - PEAR DB may not be automatically installed in your server. So you may have to install a local version if you are on a shared server (there are instructions for what to do in the PEAR manual).