This article mainly introduces the method of using PDO in the database abstraction layer in php. taking PDO for database connection, insertion, query, and other operations as an example, it analyzes the related skills of PDO in database operations, for more information about how to use PDO in the database abstraction layer, see the example in this article. We will share this with you for your reference. The details are as follows:
The test code is as follows:
<? Php/************************* @ Filename: pdotest. php @ Content: PDO MySQL, Access (test) * ***********************/if ($ _ GET ['DB'] =' mysql ') {$ dns = 'MySQL: host = localhost; dbname = test'; $ dbuser = 'root'; $ dbpass = 'root'; $ db = new PDO ($ dns, $ dbuser, $ dbpass);} else {$ db = new PDO ("odbc: Driver = {Microsoft Access Driver (*. mdb)}; Dbq = ". getcwd (). "// test. mdb ");} if ($ _ POST ['Reg ']) {$ db-> exec (" insert into t_user (Name, email) VALUES ('". $ _ POST ['name']. "','". $ _ POST ['email ']. "');"); // header ('Location :'. $ _ SERVER ['php _ SELF ']);?> Return <?} Else {$ html ='
'; $ Re = $ db-> query ("SELECT uid, name, email FROM t_user order by email;"); while ($ rs = $ re-> fetch ()) {$ userlisthtml. =''. $ Rs ['uid'].''. $ Rs ['name'].''. $ Rs ['email'].'';} $ Html. ='
User List
ID |
Name |
Email |
'. $ Userlisthtml .'
';} Echo $ html;?>
Test environment:
Php. ini file: Open extension = php_pdo_odbc.dll and remove the semicolon to open the aceess database driver.
Mysql is enabled by default.
Access path:
Mysql database
Http: // 192.168.1.21/lava_guess2009/test/pdo/pdotest. php? Db = mysql
Aceess database
Http: // 192.168.1.21/lava_guess2009/test/pdo/pdotest. php
Features: to use different databases, you only need to change the connection driver. the code does not need to be changed, that is, the benefits of the abstraction layer.
I hope this article will help you with php programming.