PHP uses PDO to operate MySQL DB instance, Pdomysql db instance _php tutorial

Source: Internet
Author: User

PHP uses PDO to operate MySQL DB instance, Pdomysql DB instance


This example describes how PHP uses PDO to manipulate MySQL databases. Share to everyone for your reference. The specific analysis is as follows:

PDO is a common class for MySQL database operations, and we do not need a custom class to manipulate the database directly using PDO, but the PDO is not open in the PHP default configuration so we must first open it in php.ini to be able to use it, here's a detailed introduction.

The PDO extension defines a lightweight, consistent interface for 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:
Copy the Code code as follows: 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:
Copy the Code code as follows: Extension=php_pdo_mysql.dll

Here is the use of PDO to the basic MySQL to delete and change the operation, the PHP program code is as follows:
Copy the 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 ']. '
';
}
Delete
$sql = "Delete from buyer where id>5";
$res = $pdo->exec ($sql);
Echo ' affects number of rows: '. $res;

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/935489.html www.bkjia.com true http://www.bkjia.com/PHPjc/935489.html techarticle PHP uses PDO to operate a MySQL database instance, Pdomysql database instance This article describes how PHP uses PDO to operate the MySQL database. Share to everyone for your reference. The specific analysis is as follows: ...

  • 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.