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, see
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, see
This example describes how php simply uses PDO at the database abstraction layer. 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
? Db = mysql
Aceess Database
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.
,