#native_company# #native_desc#
#native_cta#

PHPLib and Multiple Databases Page 5

By Peter Moulding
on November 7, 2000

Changing the code to handle multiple databases is more complex. You have to
change the query functions, as well as the connect and select. You might
want to read up on PHP’s ODBC connection, then use the ODBC option in
PHPLIB. ODBC handles many databases in a generic way that may be a little
slower. ODBC lets you use the same code on multiple types of databases. If
you do use multiple database types, you can have problems with dates
requiring different formats and generally weird differences between the
databases. ODBC simplifies the connection, but does not fix the way
databases interpret data and SQL.
Now for a quick lesson on redefining object classes. The connect() function
is wrapped in a class definition:

<?php

class DB_Sql {

}

?>



When we copy the function in to common.php3, we need to redefine the DB_Sql
class. We do that by wrapping connect() in:

<?php

class db_DB_Sql extends DB_Sql {

}

?>



Have a look at the PHP documentation on objects and classes to see what
“extends” does. The “25 words or less” summary is: Everything in the
extended definition replaces and overrides everything in the original
definition.
Now to use db_DB_Sql. When you set up PHPLIB, you will have a statement
that says:

<?php

$x = new DB_Sql;

?>



Change it to:

<?php

$x = new db_DB_Sql;

?>



That will use the modified class, instead of the original.
You are now an expert on objects, classes, OOP and can demand $10,000 more
per year.
We made an effective change with the minimum impact on the PHPLIB code.
Keep track of the changes, so you can reapply them to a new release of
PHPLIB. If you have errors with database access, you can place print
statements in the external functions to see when connections are made. You
can now do a lot more, without changing the PHPLIB code.
If the SQL seems to fail, you can copy the query() function from DB_Sql in
db_mysql.inc to the db_DB_Sql in common.PHP3, then insert a print statement
to see the SQL in use.