First understanding of PHP (4) PDO object configuration in use-how to encode Singularity 1. 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:
II. enabling PDO
PDO requires php versions later than 5.1.
To check whether the pdo function is enabled, you need to create a new php file and query it using the phpinfo function.
, PDO driver and pdo_mysql extension are enabled)
Linux:
My system is ubuntu14.04.3, and the php version is PHP 5.5.9-1ubuntu4. 14. here, the pdo extension is on by default (in the later php versions, pdo is already a core function and does not need to be installed on its own, the tutorials on the Internet are for the previous php version), and the mysql extension of php is also on by default. I don't know if this is because I use php of ubuntu. If the mysql extension is not available, you only need to execute sudo apt-get install php5-mysql to install the driver, and other databases do the same.
Win: in the php. ini file, remove the following two lines of comments.
Extension = php_pdo.dll // PDO driver shared extensions must be available (windows)
Extension = php_pdo_mysql.dll // MySQL extension
If you want to enable other database extensions, remove the corresponding annotations.
3. Use of PDO
3.1 PDO object initialization
The PDO constructor is as follows:
PDO _ construct (string dsn
[, String username
[, String password
[, Array driver_options]);
The dsn database connection information is shown in "mysql: host = localhost; dbname = database name"
The following is an example:
$ Pdo = new PDO ("mysql: host = localhost; dbname = 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:
Query data:
1
GetMessage (); 8} 9 // query statement 10 $ SQL = 'select * from students '; 11 // execute the statement and parse the data 12 echo 'id '. '____________'. 'name '. '____________'. 'SEX '."
"; 13 foreach ($ pdo-> query ($ SQL) as $ val) {14 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']."
"; 15} 16?>
Effect
Insert data:
1
GetMessage (); 8} 9 // insert statement 10 $ SQL = "insert into students values ('000000', 'aliyun', 'female ')"; 11 // execute the statement and parse the data 12 $ res = $ pdo-> exec ($ SQL); 13 if ($ res) {14 echo "insert successful!
"; 15} 16 // query result 17 $ SQL = 'select * from students '; 18 echo 'id '. '____________'. 'name '. '____________'. 'SEX '."
"; 19 foreach ($ pdo-> query ($ SQL) as $ val) {20 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']."
"; 21} 22?>
Effect
The operation for modifying or deleting data is similar.
Modify data:
1
GetMessage (); 8} 9 // insert statement 10 // $ SQL = "insert into students values ('2017010', '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!
"; 17} 18 // query result 19 $ SQL = 'select * from students '; 20 echo 'id '. '____________'. 'name '. '____________'. 'SEX '."
"; 21 foreach ($ pdo-> query ($ SQL) as $ val) {22 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']."
"; 23} 24?>
Modify data
Effect
Delete data:
1
GetMessage (); 8} 9 // insert statement 10 // $ SQL = "insert into students values ('2017010', '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 the statement and parse the data 16 $ res = $ pdo-> exec ($ SQL); 17 if ($ res) {18 echo "deleted successfully!
"; 19} 20 // query result 21 $ SQL = 'select * from students '; 22 echo 'id '. '____________'. 'name '. '____________'. 'SEX '."
"; 23 foreach ($ pdo-> query ($ SQL) as $ val) {24 echo $ val ['id']. '____________'. $ val ['name']. '____________'. $ val ['sex']."
"; 25} 26?>
Delete data
Effect
The above are the basic usage of PDO, addition, deletion, query, modification, and other operations.