PDO controls and connects to databases, and pdo controls databases.

Source: Internet
Author: User

PDO controls and connects to databases, and pdo controls databases.

PDO (PHP Data Objects) is an interface used to connect to the database in PHP. PDO and mysqli have been recommended to replace mysql-related functions used in PHP. This is based on the security of database usage, because the latter lacks protection against SQL data hidden codes.

PDO is another method for connecting to the database. Previously, mysqli was used. PDO is not just a connection to the musql database, but also a connection to databases such as sqlserve l, which is more convenient to use.

One of the biggest features of PDO is its transactional nature, which is mainly used in finance. It is implemented together or with exceptions. For example, in the financial transfer process, with the increase of finance in one place, the finance in the other place will be reduced, and the use of PDO can avoid the emergence of one Finance

Increased, while the other finance did not decrease.

You cannot use the PDO extension to perform any database operations. You must use a database-specific PDO driver (PDO driver for specific databases) to access the database server. PDO does not provide database abstraction. It does not rewrite SQL statements or provide functions missing from the database itself. If you need this function, you need to use a more mature abstraction layer.

1 <? Php 2 // create object 3 $ dsn = "mysql: dbname = test; host = localhost;"; 4 5 $ pdo = new PDO ($ dsn, "root ", "root"); 6 7 // set the Error Type of PDO to exception Mode 8 $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION ); 9 10 try {11 // start the transaction 12 $ pdo-> beginTransaction (); 13 14 $ sql1 = "insert into score VALUES ('20140901', '3-123 ', '90') "; 15 $ sql2 =" insert into score VALUES ('000000', '3-111 ', '92 ')"; // these two statements are executed together. If an error occurs, both statements cannot be executed. 16 17 $ pdo-> exec ($ sql1) ; // If the select statement is called with query (), 18 $ pdo-> exec ($ sql2) is available ); 19 20 // submit the transaction 21 $ pdo-> commit (); 22} 23 catch (Exception $ e) 24 {// rollBack operation 25 $ pdo-> rollBack (); 26 }?>

Database after database operation before the operation (after the transaction is committed, it appears in the database together)

If you want to add a large volume of data, it may be a bit difficult to write using the above method. The following is a simple method to write using the array mode.

1 // create object 2 $ dsn = "mysql: dbname = test; host = localhost"; 3 $ pdo = new PDO ($ dsn, "root", "root "); 4 // pre-processing SQL statement 5 $ SQL = "insert into score VALUES (: sno,: cno,: degree )"; 6 $ stem = $ pdo-> prepare ($ SQL); 7 8 // create an array 9 $ arr = array ("sno" => "120 ", "cno" => "95033", "degree" => 88); 10 11 // run 12 $ stem-> execute ($ arr );

Below is an example to illustrate the simplicity of this method: Write an add page and its processing page

1 // This is the add page 2 <body> 3 

Display

1 // This is the add processing page 2 <? Php 3 $ dsn = "mysql: dbname = test; host = localhost"; 4 $ pdo = new PDO ($ dsn, "root", "root "); 5 // pre-processing Statement 6 $ SQL = "insert into score VALUES (: sno,: cno,: degree)"; 7 $ tem = $ pdo-> prepare ($ SQL ); 8 // execute 9 $ tem-> execute ($ _ POST); 10?>

It can be seen from the added processing page that it is easier to process with pdo than with mysqli.

 

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.