Home Backend Development PHP Tutorial php数据库抽象层 PDO_php技巧

php数据库抽象层 PDO_php技巧

May 17, 2016 am 09:19 AM
database

下面就来介绍一下数据库抽象层PDO的使用:

PDO(PHP Data Objects)是一个轻量级的PHP扩展,提供了一个数据访问抽象层。还要就是PDO只能在PHP5.0以上版本使用。

下面来介绍一下PDO常用的预定义常量:

PDO::PARAM_BOOL (integer)    表示布尔数据类型

PDO::PARAM_NULL (integer)    表示数据类型为NULL的SQL

PDO::PARAM_INT (integer)      表示为integer数据类型的SQL

PDO::PARAM_STR (integer)      表示为char varchar 或者其他字符串的数据类型的SQL

PDO::PARAM_LOB (integer)      表示对象数据类型的SQL

PDO::FETCH_LAZY (integer)    指定的获取方式,应当返回结果集的每一行作为一个对象的变量名,对应于它的字段名

PDO::FETCH_ORI_NEXT (integer) 取结果集的下一行

PDO::FETCH_ORI_PRIOR (integer取结果集的前面的行

PDO::FETCH_ORI_FIRST (integer取结果集的第一行

PDO::FETCH_ORI_LAST (integer) 取结果集的最后一行

PDO::ATTR_PERSISTENT (integer)   创建一个持久连接,而不是新创建一个连接

PDO的基本用法:

使用PDO与数据库连接(这里只使用MySQL):

复制代码 代码如下:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>

下面的代码是对MySQL连接错误时的处理:
复制代码 代码如下:

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();
}
?>

下面是两个重复使用插入语句的例子:
复制代码 代码如下:

$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 = 'two';
$value = 2;
$stmt->execute();
?>

复制代码 代码如下:

$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 = 'two';
$value = 2;
$stmt->execute();
?>

对数据库进行查询操作:
复制代码 代码如下:

$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

How does Go language implement the addition, deletion, modification and query operations of the database?

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

Comparison of similarities and differences between MySQL and PL/SQL

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

How does Hibernate implement polymorphic mapping?

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of MySQL database management system

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

An in-depth analysis of how HTML reads the database

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

Tips and practices for handling Chinese garbled characters in databases with PHP

See all articles