Php uses the PDO method in the database abstraction layer

Source: Internet
Author: User
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. ='

'. $ Userlisthtml .'
User List
ID Name Email

';} 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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.