First understanding of PHP (4) PDO object configuration and use, first understanding of pdo

Source: Internet
Author: User

First understanding of PHP (4) PDO object configuration and use, first understanding of pdo

I. PDO Concept

PDO is actually a database abstraction layer. With PDO programming, you can easily change the database at any time in subsequent operations without changing the source code. Shows the location of PDO:

Phptest, "root", "123 ");

This completes the initialization of the PDO object. The connected database is the phptest database of the mysql database. The user names and passwords used are root and 123 respectively.

If you write the dsn information to the configuration file, use the following method:

$ Pdo = new PDO ("uri: MysqlDbo. ini", "root", "123"); \ dsn data is written in the MysqlDbo. ini file

  

3.2 use of PDO objects

The PDO member method is as follows:

1) query ($ SQL); // used to execute the query SQL statement. Returns the PDOStatement object.

2) exec ($ SQL); // used for adding, deleting, and modifying operations. The number of affected rows is returned;

3) setAttribute (); // set a "database connection object" attribute.

4) fetchAll (); // parse data

 

The following is an example:

The original database data is as follows:

1 <? Php 2 // connect to database 3 try {4 $ pdo = new PDO ("mysql: host = localhost; dbname = phptest", "root", "20125202 "); 5} 6 catch (PDOException $ e) {7 die ("database connection failed ". $ e-> getMessage (); 8} 9 // query statement 10 $ SQL = 'select * from students '; 11 // execute the statement and parse the data 12 echo 'id '. '____________'. 'name '. '____________'. 'sex '. "<br>"; 13 foreach ($ pdo-> query ($ SQL) as $ val) {14 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex ']. "<Br>"; 15} 16?>

 

Effect

1 <? Php 2 // connect to database 3 try {4 $ pdo = new PDO ("mysql: host = localhost; dbname = phptest", "root", "123 "); 5} 6 catch (PDOException $ e) {7 die ("database connection failed ". $ e-> getMessage (); 8} 9 // insert statement 10 $ SQL = "insert into students values ('2013', 'tony', 'female ')"; 11 // execute the statement and parse the data 12 $ res = $ pdo-> exec ($ SQL); 13 if ($ res) {14 echo "insert successful! </Br> "; 15} 16 // query result 17 $ SQL = 'select * from students '; 18 echo 'id '. '____________'. 'name '. '____________'. 'sex '. "<br>"; 19 foreach ($ pdo-> query ($ SQL) as $ val) {20 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']. "<br>"; 21} 22?>

Effect

1 <? Php 2 // connect to database 3 try {4 $ pdo = new PDO ("mysql: host = localhost; dbname = phptest", "root", "123 "); 5} 6 catch (PDOException $ e) {7 die ("database connection failed ". $ e-> getMessage (); 8} 9 // insert statement 10 // $ SQL = "insert into students values ('2013', 'tony', 'female ') "; 11 // modify statement 12 $ SQL =" update students set sex = 'male' where id = '000000 '"; 13 // execute the statement and parse the data 14 $ res = $ pdo-> exec ($ SQL); 15 if ($ res) {16 echo "modified successfully! </Br> "; 17} 18 // query result 19 $ SQL = 'select * from students '; 20 echo 'id '. '____________'. 'name '. '____________'. 'sex '. "<br>"; 21 foreach ($ pdo-> query ($ SQL) as $ val) {22 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']. "<br>"; 23} 24?>Modify data

Effect

1 <? Php 2 // connect to database 3 try {4 $ pdo = new PDO ("mysql: host = localhost; dbname = phptest", "root", "20125202 "); 5} 6 catch (PDOException $ e) {7 die ("database connection failed ". $ e-> getMessage (); 8} 9 // insert statement 10 // $ SQL = "insert into students values ('2013', 'tony', 'female ') "; 11 // modify statement 12 // $ SQL =" update students set sex = 'male' where id = '000000 '"; 13 // delete statement 14 $ SQL = "delete from students where id = '000000'"; 15 // execute statement and parse data 16 $ Res = $ pdo-> exec ($ SQL); 17 if ($ res) {18 echo "deleted successfully! </Br> "; 19} 20 // query result 21 $ SQL = 'select * from students '; 22 echo 'id '. '____________'. 'name '. '____________'. 'sex '. "<br>"; 23 foreach ($ pdo-> query ($ SQL) as $ val) {24 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']. "<br>"; 25} 26?>Delete data

Effect

The above are the basic usage of PDO, addition, deletion, query, modification, and other operations.

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.