Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
1. preface ADODB is short for ActiveDataObjectsDataBase. it is a function component for accessing the database in PHP. Chen Yingguang, the host of the SFS3 system (Campus Free Software Exchange Network student affairs system) plan, decided to use this component, in order to allow more partners who are interested in the project to smoothly join the development process, the younger brother believes that it is necessary to write the introduction to the Chinese language of ADODB for reference by partners. Although PHP is a powerful tool for building a Web system, the function of accessing the database by PHP has never been standardized, use a different and incompatible application interface (API ). In order to fill this gap, ADODB emerged. Once the interfaces for accessing the database are standardized, the differences between different databases can be hidden. it is very easy to switch to other different databases.
Currently, ADODB supports many database types, such as: MySQL, PostgreSQL, Interbase, Informix, Oracle, ms SQL 7, Foxpro, Access, ADO, Sybase, DB2 and ODBC (among which the driver of PostgreSQL, Informix, and Sybase is contributed by the development of the free software community ).
One of the biggest advantages of using ADODB is that, regardless of the backend database, the methods for accessing the database are the same, so developers do not have, however, you must learn another set of different access methods, which greatly reduces the knowledge burden on developers. the previous knowledge can still be used in the future. when the database platform is transferred, the program code does not have to be too much updated.
In fact, the development concept of ADODB is not the first. DBI appeared earlier than ADODB. It provides Perl to access the database and uses consistent API call interfaces. I believe that friends who have used Perl + DBI will have a similar feeling when they use ADODB again.
In addition, ADODB should be familiar to people who have used ASP. such friends should be very easy to accept.
Let's take a look at the simple usage of ADODB:
// Introduce the inc file of adodb to call the function provided by adodb
Include ('adodb/adodb. inc. php ');
// Select the connected database type to create an online object,
// Once an object is created, its member function can be used to process the database.
// The following $ conn is the object)
$ Conn = & ADONewConnection ('mysql ');
// Do you want to display the error message, false or true.
If you disable the debug function, that is, $ conn-> debug = false, the result is as follows:
The following describes how to use ADODB.
The home page of ADODB is http://php.weblogs.com/adodb. the latest version is 2.42 (). you can download it from ADODB or from the FTP center of Tainan County Education Network.
The method for installing ADODB is extremely simple. you only need to download, decompress, and place it in the appropriate location! As follows:
$ Ncftp ftp.tnc.edu.tw
Suppose I put adodb242.tgz in/var/www/html
$ Cp adodb242.tgz/var/www/html
$ Tar xvzf adodb242.tgz
... The following content is omitted ....
Now, you have installed adodb in/var/www/html/ADODB.
3. introduce ADODB
Once you have installed ADODB, you should introduce the ADODB-related inclusion files into your program before use. It doesn't matter if the adodb directory is placed anywhere, as long as it can point to the correct path file name. Generally, your program code only needs to introduce adodb. inc. php.
The procedure is as follows:
In your PHP program:
Include ('path/adodb. inc. php ');
Include_once ('path/adodb. inc. php ');
If your program and adodb are in the same directory:
Something. php *
Include ('adodb/adodb. inc. php ');
If the location is in a directory named somedir:
You must use:
Include ('../adodb. inc. php ');
In addition to the inclusion file of adodb. inc. php, ADODB also provides many inclusion files of adodb-*. inc. php, most of which are designed to drive the special usage of some databases.
The introduction of adodb-session.php allows you to store sessions in the database for maintenance and application.
If a adodb-pager.inc.php is introduced, it makes it easy for you to display by page.
If an adodb-errorhandler.inc.php is introduced, you can customize error handling messages.
If the adodb-pear.inc.php is included, you can use the pear db syntax of PHP4 to use ADODB. In this case, you can use DSN to set the string used to connect to the database. For example, $ dsn = "mysql: // piza: ooo123 @ localhost/test ";
If tohtml. inc. php is introduced, you can easily convert the retrieved records into HTML tables for display in the program code.
If toexport. inc. php is introduced, you can easily output CSV files or data files with tab-separated fields.
Rsfilter. inc. php allows you to pre-filter records before using them.
If pluttable. inc. php is introduced, you can use the lateral table function (commonly known as cross-tabulations ).
Note! Adodb. inc. php must be introduced. for others, you can introduce this include file as needed.
4. select a database type and create an online object
Because ADODB uses the object-oriented approach, you can create an online object based on the type of your backend database after introducing the file. The procedure is as follows:
Take MySQL database as an example:
$ Conn = & ADONewConnection ('mysql ');
Note: NewADOConnection and ADONewConnection are the same and both can be used.
In the above example, 'mysql' refers to the drvier type of the database, and ADODB calls the corresponding database driver accordingly.
Other commonly used drivers include: access, ado, ado_access, ado_mssql, db2, vfp, ibase, borland_ibase, informix, imformix72, mssql, oci8, odbc, IPVs, ipvs64, ipvs7, sqlanywhere, sybase .... and so on.
We call the created object $ conn as an ADOConnection object, which represents the connection transaction with the database and is processed through this object. The ADOConnection object provides many processing methods, which are object-oriented. these methods are called member functions, which are interfaces for external access to this object.
Once an online object is created, there are many object types available for you to call! See the introduction in the next section.
5. Error detection mode
In the process of program development, in order to easily find out where problems may occur, we usually turn on the error detection mode and close the program once its functions are stable. ADODB provides an error detection mode. when accessing a database, the operation mode is displayed.
To enable the error detection mode, use the following method:
$ Conn-> debug = true
Disable the error detection mode. use the following method:
$ Conn-> debug = false
6. connect to the database
Then, use the Connect or PConnect function of the $ conn online object to Connect to a specific database. in this case, Data Source Names (DSN) Data must be provided. the DSN may include: host name, database user, database password, and database name. Different database types may be omitted by DSN. For MySQL, all the above four are provided.
This function returns true or false to indicate whether the connection is successful.
In the above example, after 1st records, 3, 3, and 4 records are taken out.
The result is as follows:
(Mysql): select * from t LIMIT 1, 3
Note! SelectLimit is written in the opposite way as MySQL syntax!
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.