We accustomed to using MySQL. We like it for its simplicity, easiness of using and for programs that lighten the work (phpMyAdmin). We create scripts and afraid that customer has another SQL base. How can we create universality if our scripts don�t depend on the database? We search new classes for working with different databases.
In that article we�ll talk about DB class that provides access to different databases (MySQL, PostgreSQL, Oracle 7/8/8i, Microsoft SQL, InterBase, MiniSQL, SyBase, Informix, FrontBase and universal ODBC).
We will create connect.php file that will connect to our database in every script if it is necessary.
------------- connect.php ------------
<?PHP
require_once("DB.php");
#connect class DB PEAR

$TypeSQL= "mysql";
$Host="localhost";
$User="YourPasswordSQL"
$Name="YourNameSQL";
$Dbase="MyDBase";

# write DSN source
http://pear.sourceforge.net/manual/core.db.tut_dsn.php
$dsn="$TypeSQL://$User:$Password@Host/$Dbase"
$db= DB::connect($dsn, true);
# It conforms to: mysql_db_connect() and mysql_db_select
$db->setFetchMode(DB_FETCHMODE_ASSOC);
//line format choosing. In our case we use
// fetchRow().
?>
That�s it. If you want to use another database you have to change value of the $TypeSQL variable to "pgsql" (PostgreSQL) for example.
Now let�s create queries and start working with our database.
<?PHP
require_once("connect.php");
#Connect to the database

$sql= "SELECT * FROM MyTable";
$res= $db->query ($sql);
#Execute our query (analogy mysql_query() )
$num= $res->numRows();
#Define the number of lines (analogy mysql_num_rows() )
while($row= $res->fetchRow())
{
$id= $row["id"];
$name= $row["name"];
}
#Get the required data (analogy mysql_fetch_array() )
$res->free();
#Release memory (analogy mysql_free_result() )
?>
If you worked with other bases except MySQL you could say that MySQL has such method like auto_incremen. I�ll try to explain. If you don�t want to have any problems with changing database you don�t have to use auto_incremen method at once.
PEAR has such function as SEQUENCE that creates new sequence by means of createSequence().
I�ll try to explain simpler. In the database there is created a table with one field where will be record counter value. We need sequence when using INSERT:
<?PHP
require_once("connect.php");
$id= $db->nextID(w_, MyTable);
# That function returns the next figure that is in the sequence table.
# If sequence table hasn�t been created it should be created
# In the brackets there is a sequence table name
$sql= "INSERT INTO MyTable VALUES("$id", "$name")";
$res= $db->query($sql);
$res->free();
?>
Here we use an error handler (PEAR_ERROR_CALLBACK mode) on the base of our function. That�s why we use here our connect.php file.
<?PHP

define
(DEBUG_ENV, true);
function
handle_pear_error($error_obj)
{
if(
DEBUG_ENV)
{
die(
$error_obj->getMessage()."

"
.$error_obj->getDebugInfo());
}
else
{
die(
"Your query isn�t fulfilled. Try later.");
}
}
# Error handling function
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "handle_pear_error");
#if there is an error our function must be used
?>