The PDO extension defines a lightweight, consistent interface for the PHP Access database, which provides a data access abstraction layer,
This way, no matter what database you use, you can execute queries and fetch data through consistent functions.
The PHP version supported by PDO is PHP5.1 and a higher version, and under PHP5.2 PDO defaults to the open state,
The following is the configuration of PDO in php.ini:
Extension=php_pdo.dll
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 turn on the following extensions
Extension=php_pdo_mysql.dll
Here is the use of PDO to MySQL for basic additions and deletions to check operations
Program code:
The code is as follows |
Copy Code |
Header (" Content-type:text/html;charset=utf-8 "); $dsn = "Mysql:dbname=test;host=localhost"; $db _user= ' root '; $db _pass= ' admin '; try{ $pdo =new PDO ($DSN, $db _user, $db _pass); } catch (Pdoexception $e) { echo ' database connection failed '. $e->getmessage () //New $sql = INSERT INTO buyer (username,password,email) VALUES (' FF ', ' 123456 ', ' admin@admin.com '); $res =$ Pdo->exec ($sql); Echo ' affects the number of rows: '. $res; //modify $sql = "Update buyer set username= ' ff123 ' where id>3"; $res = $pdo->exec ($sql); Echo ' affects number of rows: '. $res; //Query $sql = "SELECT * from Buyer"; $res = $pdo->query ($sql); foreach ($res as $row) { echo $row [' username ']. ' <br/> '; } //delete $sql = "Delete from buyer where id>5"; $res = $pdo->exec ($sql); Echo ' affects number of rows: '. $res; /td> |