hostname (for non ODBC), and a database name. So complete DSN syntax
would be
driver name://username:password@hostname/databasename
PEAR DB has a database driver for MySQL called
mysql
, it can be used to
create a connection. So DSN for connecting to our database would be
mysql://itso:itso@localhost/itsodb
PEAR DB does not provide a driver for DB2 UDB. But it supports DB2 UDB
using ODBC driver. SO DSN for DB2 UDB would be
odbc(db2)://itso:itso/itsodb2
You can optionally provide second parameter to
connect()
which specifies
whether you want your connection to be persistent or non persistent.
Example 7 35 shows the PEAR DB database connect for MySQL and the
DB2 conversion code.
Example 7 35 PEAR DB database connection for DB2 UDB and MySQL
include( /usr/local/lib/php/PEAR/DB.php );
\\ MySQL database connection with DSN='mysql//itso:itso@localhost/itsodb2'
$conn = DB::connect( mysql//itso:itso@itsodb2 );
\\ DB2 UDB database connection with DSN='odbc(db2)//itso:itso/itsodb2'
$conn = DB::connect( odbc(db2)://itso:itso@itsodb2 );
if(DB::isError($conn))
print "connection error : ".$conn >getMessage();
Process query
The query can be executed by calling
query()
method on the connection
object. It takes the SQL statement as an input parameter and returns the
results. The results can be used in three modes depending on the SQL
statement and statement's final status:
Error case
In case an error occurs while executing the SQL statement you can use
return object for getting error message using the method
getMessage()
.
This code does not change with the change in database, so MySQL and
DB2 UDB code is same. Example 7 36 shows the usage of the result
object for getting the error message.
Result set
If your SQL statement was a query statement, the return object can be
used to fetchthe result set using the fetchRow variable of the return object.
Again, MySQL and DB2 UDB database differences do not cause any
Chapter 7. Application porting
187
footer
Our partners:
PHP: Hypertext Preprocessor Cheap Web Hosting
JSP Web Hosting
Ontario Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Java Hosting
Cheapest Hosting
Visionwebhosting.net Business web hosting division of Vision Web Hosting Inc.. All rights reserved