Article Title: PEAR: Steps for creating an intermediate database application layer. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. What is a DB class? First, let's take a brief look at the DB class. The DB class is a collection of several classes for data operations in PEAR. Its main purpose is to provide a unified and abstract data interface, which is irrelevant to the backend database. Therefore, if your application uses this universal interface for database operations, you can smoothly switch to different databases, such as MYSQL, SQL, SYBASE, and so on. In fact, the DB class hopes to play a simple role similar to ODBC or DBI in PERL. Speaking of this, I have to mention another excellent library in PHP: ADODB. Like DB, ADODB provides an abstract middle layer, and ADODB supports more backend databases than DB (at least currently). However, ADODB does not directly use PEAR features, it only draws on many PEAR ideas, including DB, so there are many similarities between the two. I don't want to comment on the advantages and disadvantages of both. You can use them based on your preferences.
2. Why design an abstract intermediate data layer? Before discussing the usage of the database in detail, we should first discuss why we should design the intermediate data layer, because this means that you need to make some sacrifices and concessions. For example, you need to write more code, some features limited to specific databases cannot be directly used.
Let's recall our past practices and how to connect to the MYSQL database? This is indeed a problem with pediatrics. You must be familiar with the following code:
/**
* Connect to the MYSQL database
*/
$ Host = "localhost ";
If ($ persisternt ){
$ Conn = mysql_connect ($ host, $ user, $ passwd );
} Else {
$ Conn = mysql_pconnect ($ host, $ user, $ passwd );
}
?>
Now we have established a database connection. We can use it for database operations. We may use similar code:
Function SQL _exec ($ SQL ){
Global $ db_Name;
$ Result = mysql_db_query ($ db_dbName, $ SQL );
If (! $ Result ){
Echo mysql_errno (). ":". mysql_error ()."
$ SQL
";
Exit ();
}
Return $ result;
}
$ Db_Name = "test ";
$ SQL = "select * from users ";
$ Result = SQL _exec ($ SQL );
While ($ row = mysql_fetch_row ($ result )){
Echo "Name: $ row [0] gender: $ row [1] age $ row [2]
";
}
Mysql_free_result ($ result );
?>
Looks good, right? You may use many similar code snippets in your code. But don't be so happy. The problem is coming. Suppose, suddenly, your database needs to be migrated from MYSQL to another database platform, such as ORACLE and SYBASE. There are many reasons for migration. Maybe your boss has a whim and thinks it can sell for a good price, or your data surge may lead to a decline in MYSQL performance. In short, migration is required. You may think about how you do it. It's simple. Just replace the relevant functions.
It sounds simple, ...... First, to change the database connection function, replace mysql_connect and mysql_pconnect with OCILogon and OCIPLogon. Mysql_errno and mysql_error () cannot be used. You need to extract the response information from the array returned by OCIError.
This is not too bad. The worst thing is that the related mysql_fetch_row and mysql_fetch_array statements are distributed in many of your code functions and processes. You need to find and analyze them one by one, then replace or write the corresponding ORACLE version. If your database operations are concentrated in a certain module or class, this work is acceptable. Otherwise, it means you read and modify most of the code again. Even if this unfortunate person is not you, he will secretly curse you; =)
As mentioned above, we recall our previous practices and possible misfortunes. So what should I do if I use the DB class for similar operations? The following is the database version code:
Include_once "DB. php ";
/*
* Connect to the database
*/
$ Db_host = "localhost ";
$ Db_user = "root ";
$ Db_passwd = "";
$ Db_dbName = "test ";
$ PersistentConnection = 1;
$ Db_type = "mysql ";
$ Db_proto = "";
$ Db = DB: connect ("$ db_type: // $ db_user @ $ db_passwd: $ db_host/$ db_dbName", $ db_options );
If (DB: isError ($ db )){
Die "cannot connect to the database. Error cause:". DB: errorMessage ($ db );
}
Function SQL _exec ($ SQL ){
Global $ db;
$ Result = $ db-> query ($ SQL );
If (DB: isError ($ result )){
Echo "Database Error:". DB: errorMessage ($ result );
Exit ();
}
Return $ result;
}
//////////////////////////////////////// /////
$ SQL = "select * from users ";
$ Result = SQL _exec ($ SQL );
While ($ row = $ result-> fetchRow ()){
Echo "Name: $ row [0] gender: $ row [1] age $ row [2]
";
}
?>
In addition to connecting to the database, others seem to have only some minor changes. Error Handling uses the PEAR-like method (isError), which is actually inherited from PEAR. In the same situation, if you want to migrate the database from mysql to another form, this time it is assumed that it is PostegreSQL, a very good database in LINUX, all you do is change a line of code:
$ Db_type = "mysql ";
To:
$ Db_type = "pgsql ";
Otherwise, do not change. How is the upgrade feeling refreshing? You can study the rest of the code with the rest of the time, or continue to discuss the usage of DB with me.
Iii. Getting started with DB The DB class consists of three parts:
DB. php is a front-end interface. Many "static" public methods are provided in the DB class. We generally only need to export the de_once file.
DB/common. php is a general abstract class of the backend database. The backend classes of different databases need to inherit and implement the public methods and attributes defined in this class. If your database is not supported, you can write a Support class by yourself so that your application can be migrated.
DB/storage. php is an auxiliary tool that can return SQL queries as objects and maintain these objects. When the objects change, the database is updated accordingly.
DB/ifx. php
Mssql. php ms SQL Server support Class
Oci8.php Orcale 8i support Class
Pgsql. php PostegreSQL support Class
Sybase. php Sybase support Class
Ibase. php ibase support Class
Msql. php mSQL support Class
Mysql. php mysql support
Odbc. php odbc support
These are the support classes for the corresponding back-end databases. Specific database operations are implemented by these support classes.
Next, we will first detail some "static" methods in DB. PHP:
Connect () method
This method is the most important static method. We get a DB_COMMON object and connect it to the corresponding database. The prototype of this method is as follows:
Function & connect ($ dsn, $ options = false)
$ Dsn is the abbreviation of the data source name. It can be a string or a specific array.
In general, $ dsn is a string in the following format:
* Phptype: php backend database type name (such as mysql and odbc .)
* Dbsyntax: the SQL syntax standard used by the database, which is generally not used.
* Protocol: the communication protocol used. (Such as tcp, unix, etc .)
* Hostspec: Description of the host where the database is located. (Form: host name [: port number])
* Database: database Name.
* Username: the username used for login.
* Password: the logon password.
$ Options is a hybrid database option. If the value is Boolean, this parameter indicates whether to use persistent connect. If the value of $ options is TRUE, the persistent connection is used. If it is an array, it indicates this is a specific backend database option. These options will be passed to the set_option method in the DB_common class. The backend database implements or reloads this method, you can decide how to use these options.
IsError ($ value)
This method is used to determine whether the results returned by some DB methods are an error object. You can use this method to determine whether the results of an operation throw an exception.
Of course, if your application inherits from PEAR, you can also directly use the PEAR isError to judge, especially when the exception thrown in your program may be an exception other than the database, this method can only determine whether it is a DB_Errro object, other PEAR_Err
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.