This example describes the way PHP uses PDO to manipulate MySQL databases. Share to everyone for your reference. The specific analysis is as follows:
PDO is a MySQL database operation of a common class, we do not need to customize the class can directly use PDO to manipulate the database, but in the PHP default configuration PDO is not open so we have to open it in the php.ini before it can be used, here to give a detailed description.
The PDO extension defines a lightweight, consistent interface for PHP access to a database that provides a data access abstraction layer so that you can execute queries and fetch data through consistent functions, regardless of the database you use.
The PHP version supported by PDO is PHP5.1 and a higher version, and the default is open under PHP5.2 PDO.
The following is the configuration of PDO in php.ini:
Copy Code code as follows:
To enable support for a database, you need to open the appropriate extension in the PHP configuration file, for example to support MySQL, and you need to turn on the following extensions:
Copy Code code as follows:
Extension=php_pdo_mysql.dll
Here is the use of PDO to MySQL for basic additions and deletions to check operations, PHP program code is as follows:
Copy Code code as follows:
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 number of rows: '. $res;
Modify
$sql = "Update buyer set username= ' ff123 ' where id>3";
$res = $pdo->exec ($sql);
Echo ' affects number of rows: '. $res;
Inquire
$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;
I hope this article will help you with your PHP programming.