ADODB installation, use of ADODB, usage of PHPadodb, use of adodo in PHP, PHP communication, PHP blog, PHP technical exchange, PHP Technical Blog
In PHP website development, for the database platform portability solution, we have previously introduced the pear db Class installation and use of the PHP Tutorial. In fact, using the PHP ADOdb class library is also a good solution, it supports more database engines.
As the first article in the PHP ADOdb user manual, this article mainly introduces the advantages of using the PHP ADOdb class library, the basic installation and usage of the PHP ADOdb class library, and several methods for establishing a Mysql database connection using PHP ADOdb.
Advantages of using PHP ADOdb
1. good combination with PHP, fast speed
2. good scalability, easy for programmers on the windows platform, because it is similar to the class library of Microsoft's ADO
3. easy to use
4, each version is in Access, MySQL, PostgreSQL, ms SQL, Oracle 11g independent test, quality assurance, support a variety of databases.
5. PHP4 supports using the PHP ADOdb class library to store session variables, which facilitates extension and transplantation.
Description of PHP ADOdb class library files
Adodb. inc. php is the main file. when using the adodb class, you only need to include this file.
Adodb-*. inc. php is the specific database driver code
Test. php contains a list of Test databases for testing the adodb class library, which is under the tests directory.
Adodb-session.php is the session processing code in PHP4
Testdatabases. inc. php contains a list of databases used for testing and is included by Test. php.
Tute.htm is a PHP ADOdbadodb class English version tutorial.
How to install and use the PHP ADOdb class library
1. first download the PHP ADOdb class library. The current version of The ADOdb class library is adodb5.
2. start installing the PHP ADOdb class library.
There are two main ways to install the PHP ADOdb class library: one is to contain the complete source code and the other is to minimize the installation.
First, make sure that the PHP version is 4.0.5 or above, decompress the PHP ADOdb class library file to the relevant directory of the web server, and the PHP ADOdb class library installation is complete.
Minimal installation of PHP ADOdb class library
The following files must be included in the minimal installation of the PHP ADOdb class library:
Adodb. inc. php
Adodb-lib.inc.php
Adodb-time.inc.php
Drivers/adodb-$ database. inc. php
License.txt
Adodb-php4.inc.php
Adodb-iterator.inc.php
Installation options:
Adodb-error.inc.php and lang/adodb-$ lang. inc. php (MetaError () can be used ())
Adodb-csvlib.inc.php (if you plan to use cache records, you need to use CacheExecute () and so on)
Adodb-exceptions.inc.php and adodb-errorhandler.inc.php (if you use adodb to handle errors or errors in PHP5 ).
Adodb-active-record.inc.php (Active Records claim to be able to database tables and Records and local PHP object independent, so that programmers will focus more on data processing, rather than SQL statements, the taste of MVC, haha ).
After installing the PHP ADOdb class library, let's start using the PHP ADOdb class library.
How to establish a Mysql database connection using PHP ADOdb
The PHP ADOdb class library supports multiple types of databases. this tutorial first introduces the most basic method of using the PHP ADOdb class library to connect to the Mysql database, which is similar to the pear db Class Library, the PHP ADOdb class library can also be used to establish a Mysql database connection (connect). one is to use the ADONewConnection and Connect functions to establish a connection, and the other is to use DSN to establish a connection.
Use PHP ADOdb to establish Mysql connection method 1
1 2 3 4
|
Debug = true; // debug to display specific SQL statements $ Db-> Connect ('localhost', 'root', '123', 'test '); $ Rs = $ db-> Execute ('select * from leapsoul '); Print" |
"; print_r($rs->GetRows()); print "
";
?>
Note
1. when using the PHP ADOdb class library to establish a Mysql database connection, you need to use the ADONewConnection ($ driver) function of the PHP ADOdb class library to create a connection. which database can you choose to establish, for example, Access. here I use the Mysql database to establish a connection.
2. then you need to decide whether to use a permanent connection or a non-permanent connection. the permanent connection speed is faster and the database connection will not be closed unless you use the close () function; non-permanent connections consume less resources as soon as possible, but the risk is that the load on databases and web servers is heavy. You can decide based on the specific needs of the project.
3. Execute the SQL statement using the Execute function, and then display the query information about www.leapsoul.cn in the leapsoul table in the array structure.
Knowledge Point:
1. NewADOConnection ($ driver) is the alias of ADONewConnection ($ driver). its functions are the same. when a connection is established, there is no difference between ADONewConnection ($ dbdriver) and & ADONewConnection ($ dbdriver.
2. use $ conn-> PConnect () for permanent connection, use $ conn-> Connect () for non-permanent connection, and support NConnect () for some databases (), this function forces a new database connection.
3. if you create both permanent and non-permanent connections and use the same userid and password, PHP will share the same connection. when they connect to different databases, the problem may occur. the solution is to always use different userids for different databases or use NConnect ()
Use PHP ADOdb to establish Mysql connection method 2
PHP ADOdb class library supports database connection in DSN mode starting from version 4.51
The DSN format is as follows:
1
|
$ Driver: // $ username: $ password @ hostname/$ database? Options [= value] |
Options mainly has the following options:
Options supported by all databases: 'persist', 'persistent', 'debug', 'fetchmode', 'new', 'cachesecs ', 'memcaccache'
Interbase/Firebird: 'did', 'charset', 'buffers', 'role'
M'soft ADO: 'charpage'
MySQL: 'clientflags'
MySQLi: 'port', 'socket ', 'clientflags'
Oci8: 'nls _ date_format ', 'charset'
If the options value is not assigned a value, the default value is 1.
Cachesecs determines how many seconds the recordsets caches when the CacheExecute () and CacheSelectLimit () functions are called and the cache-time parameters are not set for these two functions. the default cache duration is 3600 seconds.
Memcache defines the host address and port of memcache and whether the compression mechanism is used.
The cachesecs and memcache parameters have been added since PHP ADOdb 5.09.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<? Include ('adodb5/adodb. inc. php ');
# Non-permanent connection mode $ Dsn = 'MySQL: // root: 123456 @ localhost/test '; $ Db = NewADOConnection ($ dsn ); If (! $ Db) die ("Connection failed ");
# Permanent connection // $ Dsn2 = 'MySQL: // root: pwd @ localhost/mydb? Persist ';
# Non-permanent connection. the database port is 3000. // $ Dsn2 = 'mysqli: // root: pwd @ localhost/mydb? Persist = 0 & port = 3000 ';
$ Db-> debug = true;
$ Rs = $ db-> Execute ('select * from leapsoul ');
Print""; print_r($rs->GetRows()); print " "; ?> |
Knowledge Point:
NewADOConnection () internally calls the Connect () or PConnect () function when using DSN to Connect to the database. if the connection fails, False is returned.
So far, after installing the PHP ADOdb class library, we will introduce how to use the PHP ADOdb class library to establish a connection to the Mysql database. the connection methods for most databases are the same as those for Mysql databases, next time, we will introduce the connection methods for Access and other databases in the PHP ADOdb user manual.