PDO operation MySQL Basic tutorial share

Source: Internet
Author: User

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 (ten) not NULL DEFAULT ' 0 ',
' user ' char (DEFAULT NULL),
PRIMARY KEY (' id '),
KEY ' idx_age ' (' id ')
) Engine=innodb DEFAULT Charset=utf8;


Program code:

<?php
header ("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 ();
}
//New
$sql = "INSERT INTO Test (Id,user) VALUES (1, ' phpthinking ')";
$res = $pdo->exec ($sql);
echo ' affects number of rows: '. $res;
//Modify
$sql = "Update test set user= ' phpthinking ' where id=1";
$res = $pdo->exec ($sql);
echo ' affects number of rows: '. $res;
//Enquiry
$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;

PDO operation MySQL Basic tutorial share

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.