This article mainly introduces the PHP PDO operation method of MySQL, combined with the case of a detailed analysis of PHP open PDO and database creation, connection and increase and deletion of related operation skills, the need for friends can refer to the next
Specific as follows:
The PDO extension in PHP defines a lightweight, consistent interface to the PHP Access database, which provides a data access abstraction layer so that queries and data can be executed through a consistent function, regardless of the database used.
The PHP version supported by PDO is PHP5.1 and higher, and the PDO is turned on by default under PHP5.2,
The following is the configuration of PDO in php.ini:
Extension=php_pdo.dll
In order to enable support for a database, you need to open the appropriate extension in the PHP configuration file, for example, to support MySQL, you need to open the following extension
Extension=php_pdo_mysql.dll
Here are the basic additions and deletions to MySQL using PDO
Create the test database, and then run the following SQL statement:
DROP TABLE IF EXISTS ' test '; CREATE TABLE ' test ' (' id ' int () not NULL default ' 0 ', ' user ' char ') default Null,primary key (' ID '), key ' Idx_age ' (' ID ') )) Engine=innodb DEFAULT Charset=utf8;
Program code:
<?phpheader ("Content-type:text/html;charset=utf-8"); $dsn = "Mysql:dbname=test;host=localhost"; $db _user= ' root ' ; $db _pass= ' admin123 '; try{$pdo =new PDO ($DSN, $db _user, $db _pass);} catch (Pdoexception $e) {echo ' Database connection failed '. $e->getmessage ();} Added $sql= "INSERT into Test (Id,user) VALUES (1, ' phpthinking ')"; $res = $pdo->exec ($sql); Echo ' Affects rows: '. $res;//Modify $sql= " Update test set user= ' phpthinking ' where id=1 "; $res = $pdo->exec ($sql); Echo ' Affects rows: '. $res;//Query $sql=" SELECT * FROM Test " ; $res = $pdo->query ($sql); foreach ($res as $row) {echo $row [' user ']. ' <br/> ';} Delete $sql= "Delete from Test where id=1"; $res = $pdo->exec ($sql); Echo ' affects number of rows: '. $res;
Related recommendations:
A case analysis of PDO database operation class in PHP encapsulation
The method of implementing SQLite operation class based on PDO in PHP
Definition and usage of PDO public class in PHP