PHP Database Abstraction Layer pdo_php Tutorial

Source: Internet
Author: User
Tags php database
Here's a look at the use of the database abstraction layer PDO:

PDO (PHP Data Objects) is a lightweight PHP extension that provides an abstraction layer of data access. Also, PDO can only be used in versions above PHP5.0.

Here's a look at the predefined constants commonly used by PDO:

PDO::P aram_bool (integer) represents a Boolean data type

PDO::P aram_null (integer) represents a SQL with a data type of NULL

PDO::P aram_int (integer) represented as an integer data type of SQL

PDO::P aram_str (integer) SQL that represents the data type of a char varchar or other string

PDO::P aram_lob (integer) represents an object data type of SQL

Pdo::fetch_lazy (integer) The specified fetch method, which should return each row of the result set as the variable name of an object, corresponding to its field name

pdo::fetch_ori_next (integer) takes the next row of the result set

pdo::fetch_ori_prior (integer) takes the preceding row of the result set

pdo::fetch_ori_first (integer) takes the first row of the result set

pdo::fetch_ori_last (integer) takes the last row of the result set

pdo::attr_persistent (integer) to create a persistent connection instead of creating a new connection

Basic usage of PDO:

Use PDO to connect to the database (only MySQL is used here):
Copy CodeThe code is as follows:
$DBH = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass);
?>

The following code is the processing of a MySQL connection error:
Copy CodeThe code is as follows:
try {
$DBH = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass);
foreach ($dbh->query (' SELECT * from FOO ') as $row) {
Print_r ($row);
}
$DBH = null;
} catch (Pdoexception $e) {
Print "error!:". $e->getmessage (). "
";
Die ();
}
?>

Here are two examples of repeated use of INSERT statements:
Copy CodeThe code is as follows:
$stmt = $dbh->prepare ("INSERT into REGISTRY (name, Value) VALUES (: Name,: Value)");
$stmt->bindparam (': Name ', $name);
$stmt->bindparam (': Value ', $value);

Insert one row
$name = ' one ';
$value = 1;
$stmt->execute ();

Insert another row with different values
$name = ' both ';
$value = 2;
$stmt->execute ();
?>

Copy CodeThe code is as follows:
$stmt = $dbh->prepare ("INSERT into REGISTRY (name, value) VALUES (?,?)");
$stmt->bindparam (1, $name);
$stmt->bindparam (2, $value);

Insert one row
$name = ' one ';
$value = 1;
$stmt->execute ();

Insert another row with different values
$name = ' both ';
$value = 2;
$stmt->execute ();
?>

To perform a query operation on a database:
Copy CodeThe code is as follows:
$stmt = $dbh->prepare ("select * from REGISTRY where name =?");
if ($stmt->execute (Array ($_get[' name '))) {
while ($row = $stmt->fetch ()) {
Print_r ($row);
}
}
?>

http://www.bkjia.com/PHPjc/323384.html www.bkjia.com true http://www.bkjia.com/PHPjc/323384.html techarticle Here's a look at the use of the database Abstraction Layer PDO: PDO (PHP Data Objects) is a lightweight PHP extension that provides a data access abstraction layer. And the only thing that PDO can do is ...

  • Related Article

    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.