PHP-------PDO: Data access Abstraction Layer

Source: Internet
Author: User
Tags dsn

PDO: Data Access Abstraction Layer

It is used to do data access, connect to the database, execute some SQL statements

This way is more functional than the previous, Mysqli way

Use a picture to illustrate:

An artificial SQL statement was written by the Mysqli object (the encapsulated Class), the MySQL driver, and then the MySQL database. This is the way it used to be.

If this SQL statement wants to access another database, not MySQL, and want to use the Oracle call database or SQL Server database, logically, there should be a class that specifically operates on the Oracle call database

The same picture shows:

This SQL statement, through the class, finds the driver for the Oracle call database and, through the driver, accesses the Oracle call database

Also want to access another database, want to use the SQL Server database, according to logic, there should be another class is dedicated to the operation of SQL Server database,

The same picture shows:

Also want to access other databases, and then again

By the visible, in the middle of each class, it has the function is not the same, mysqli function is connected to the MySQL database, other classes, this class is connected to the Oracle call database, and then a class, this class is to operate the SQL Server database. If you want to change a database, because all of the mysqli do, to modify the code. In this case, just want to be able to have a thing, can use an SQL statement, can manipulate, all the database. In other words, the database does not have a relationship, the program can still be used, but a keyword can be changed.

With a database access statement, you can change to access multiple databases, using the same access statement, only need to change the name of the database, you can access multiple databases, the others do not need to move, just change the name of the database can be, this is to let the program, to the later, if you want to change the database, or when the program , it is very convenient to use the contents of two databases.

So it came out, PDO,

PDO is called the data Access abstraction Layer

He actually, in the middle of this piece, merge into the same, it uses a class, this class, the encapsulation of a lot of methods, using this class, to operate all the following databases, as long as PHP loaded the database driver, it can be, if not installed the driver of this database, it is not operational, As long as the driver is installed, PDO can use a statement to operate all the database, want to change which, you can change the name.

PDO: Data Access Abstraction Layer

It actually, is to merge the middle piece into the same, it uses a class, the class encapsulates a lot of methods, using this class, the class inside the encapsulation of a lot of methods, the use of classes to operate all the following databases, as long as PHP loaded database driver, you can, if not installed database driver is not allowed to operate, As long as the driver is installed, PDO can use the same statement, to operate all the database, want to change which, change the name, you can.

Data access abstraction layer, equivalent to an abstract class, let the user see this part, is actually an abstract, does not involve a specific, how to connect the database, the other database how to connect, do not involve a specific content, is an abstract, make a connection, do not control how to connect like where, Just make a connection, just write the SQL statement, execute the SQL statement will be finished, completely no tube, the following is what database. For example, using mysqli, to connect mysqli may use the method is mysqli inside, if you want to connect to another database may have another class, another class method, also different, such a program is not able to access two databases, if you want to access the code needs to modify the style.

But using PDO, so that the code style unchanged, just change the key words, you can, the code is how or what, the query is to use this method to execute, all the database query is to use this method to execute, so, the data Access abstraction layer, let our program extensibility more good.

How does the data Access abstraction layer work???

It is also a class, this class is called PDO, so before we use it, we need to build an object, and also the object of PDO,

$pdo =new PDO (); () parameters are required in parentheses

$DSN the data source, $usernane the user name of the database, $passwd the password for the database,

An array is required for the array $options parameter

DSN data source How to write it??

If you are connecting to a MySQL database, the name of the driver is MySQL, and if you are connecting to an Oracle call interface database, the name of the driver is OCI; If you are connected to a Microsoft SQL server/sql Azure Database, The name of the driver is called sqlsrv; which database to choose, which driver name to select

Example:

(Driver name)mysql(数据库名称)dbname (服务器地址)host

   

数据源是字符串

 

把数据源添加到它的第一个参数里

造了一个PDO的对象:$pdo = new PDO($dsn,"root","");

然后写SQL语句,再去执行SQL语句

The query method is to execute an SQL statement

exec method, if it is deleted and modified can be used, this method

If it is a query, you can use the Query method

1<?PHP2 3 //PDO: Data access Abstraction Layer4 //DSN: Data source;5 $dsn= "Mysql:host=localhost;dbname=lianxi";//is a string6 //made a PDO object7 $pdo=NewPDO ($dsn, "Root", "" ");//"root" user name "" Password8 9 //write SQL statementsTen $sql= "SELECT * from Kemu";//Query Statements One //Execute SQL statement A //pdo provides a way to perform -  - $a=$pdo->query ($sql);//after execution, there is a return value of $ A the //$a is something that is not an array, can you go round it -  - foreach($a  as $v)//It feels like it's a two-dimensional array - { +   Var_dump($v);  -      + } A  at?>

The results displayed:

1<?PHP2 3 //PDO: Data access Abstraction Layer4 //DSN: Data source;5 $dsn= "Mysql:host=localhost;dbname=lianxi";//is a string6 //made a PDO object7 $pdo=NewPDO ($dsn, "Root", "");8 9 //Write SQL statementsTen $sql= "INSERT into KEMU values (' 6 ', ' geography ')";//Add Data One //Execute SQL statement A //pdo provides a way to perform -  - $a=$pdo-exec($sql);//after execution, there is a return value of $ A the //$a is something that is not an array, can you go round it -  - Var_dump($a); -  +?>

The results displayed:

The above query method and the Exec method are not recommended, but will.

PHP-------PDO: Data access Abstraction Layer

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.