PDO 学习与使用 ( 二 ) PDO 数据提取 和 预处理语句
以数据库 msg 为例,说明 PDO 的数据提取、预处理语句:
mysql> show tables;
+---------------+
| Tables_in_msg |
+---------------+
| message |
+---------------+
mysql> show create table message \G
*************************** 1. row ***************************
Table: message
Create Table: CREATE TABLE `message` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(200) NOT NULL COMMENT '聊天内容',
`flag` int(11) NOT NULL DEFAULT '0' COMMENT '0-未读 1-已读',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
mysql> show columns from message;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| content | varchar(200) | NO | | NULL | |
| flag | int(11) | NO | | 0 | |
+---------+--------------+------+-----+---------+----------------+
mysql> select * from message;
+----+---------+------+
| id | content | flag |
+----+---------+------+
| 1 | hello | 1 |
| 2 | world | 1 |
+----+---------+------+
从表中选择数据
<?php $dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $stmt = $db_conn->query('select * from message');
while($row = $stmt->fetch()) {
echo $row['id'],'.',$row['content'],'<br />';
}
返回:
1.hello
2.world
说明:
① $db_conn 是 PDO 对象
② $stmt 是 PDOStatement 对象,它代表查询,并获取结果(stmt 有预处理的意思)
③ PDOStatement::fetch() 方法可以 处理大量提取数据的模式
④ fetch() 方法将从结果集中取出一行数据
数据提取模式
fetchAll() 方法可以一次检索所有的行。同时 fetch() 方法 和 fetchAll() 方法 都可以接受 fetch_style 参数,包括:
PDO::FETCH_ASSOC,返回关联数组
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $stmt = $db_conn->query('select * from message'); $res = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($res);
返回:
array
'id' => string '1' (length=1)
'content' => string 'hello' (length=5)
'flag' => string '1' (length=1)
或(fetchAll):
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $stmt = $db_conn->query('select * from message'); $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($res);
返回:
array
0 =>
array
'id' => string '1' (length=1)
'content' => string 'hello' (length=5)
'flag' => string '1' (length=1)
1 =>
array
'id' => string '2' (length=1)
'content' => string 'world' (length=5)
'flag' => string '1' (length=1)
PDO::FETCH_NUM,返回索引数组:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $stmt = $db_conn->query('select * from message'); $res = $stmt->fetchAll(PDO::FETCH_NUM);
var_dump($res);
返回:
array
0 =>
array
0 => string '1' (length=1)
1 => string 'hello' (length=5)
2 => string '1' (length=1)
1 =>
array
0 => string '2' (length=1)
1 => string 'world' (length=5)
2 => string '1' (length=1)
PDO::FETCH_BOTH,返回关联数组和索引数组:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $stmt = $db_conn->query('select * from message'); $res = $stmt->fetchAll(PDO::FETCH_BOTH);
var_dump($res);
返回:
array
0 =>
array
'id' => string '1' (length=1)
0 => string '1' (length=1)
'content' => string 'hello' (length=5)
1 => string 'hello' (length=5)
'flag' => string '1' (length=1)
2 => string '1' (length=1)
1 =>
array
'id' => string '2' (length=1)
0 => string '2' (length=1)
'content' => string 'world' (length=5)
1 => string 'world' (length=5)
'flag' => string '1' (length=1)
2 => string '1' (length=1)
参数和预处理语句
提取 id = 1 的 message 的信息,要使用一个 预处理语句,告诉 MySQL 这条语句的哪些部分是变量。在运行 PDO::query() 时,组合了 预处理 和 执行步骤:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'select * from message where id = :msg_id';
$stmt = $db_conn->prepare($sql); //执行预处理
$stmt->execute(array('msg_id'=>2));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($res);
返回:
array
0 =>
array
'id' => string '1' (length=1)
'content' => string 'hello' (length=5)
'flag' => string '1' (length=1)
说明:
① 通过传递 SQL 语句作为参数, PDO 对象的 prepare() 方法创建了 PDOStatement($stmt);
② :msg_id 前面的冒号表示这是一个占位符(placeholder);
③ 在实际查询前,会用真正的值来替换占位符
④ execute() 方法执行查询
⑤ 必须为 SQL 中的每一个占位符传入值,需要创建一个与占位符数量相同的元素组成的数组,每个占位符都有一个与之匹配的数组元素,数组元素的键名和占位符一致,数组元素的实际值替换占位符
占位符的另外一种形式:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'select * from message where id = ? and flag = ?';
$stmt = $db_conn->prepare($sql); //执行预处理
$stmt->execute(array(2, 1));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($res);
返回:
array
0 =>
array
'id' => string '2' (length=1)
'content' => string 'world' (length=5)
'flag' => string '1' (length=1)
说明:
占位符可以不需要名字,而用 ? 为变量保留一个位置作为没有命名的占位符。
当使用预处理时,为占位符传入的值已经溢出(删除了不需要的字符),因为 MySQL 知道这些都是可能改变的值。
绑定值和预处理语句的变量
当使用 不同的值 重复调用 相同的查询 时,只会有很小的系统弄个开销,可以设置一些元素用于每次查询 - PDOStatement::buildValue():
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'select * from message where id = :msg_id and flag = :msg_flag';
$stmt = $db_conn->prepare($sql); //绑定值
$stmt->bindValue(':msg_id', 1); $stmt->bindValue(':msg_flag', 1);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->bindValue('msg_id', 2);
$stmt->execute();
$res2 = $stmt->fetch(PDO::FETCH_ASSOC); var_dump($res);
var_dump($res2);
输出:
array
'id' => string '1' (length=1)
'content' => string 'hello' (length=5)
'flag' => string '1' (length=1)
array
'id' => string '2' (length=1)
'content' => string 'world' (length=5)
'flag' => string '1' (length=1)
还可以使用 PDOStatement::bindParam() 将参数和变量绑定:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'select * from message where id = :msg_id and flag = :msg_flag';
$stmt = $db_conn->prepare($sql); //绑定值
$stmt->bindParam(':msg_id', $msgid); $stmt->bindValue(':msg_flag', 1);
$msgid = 1;
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->bindValue('msg_id', 2);
$stmt->execute();
$res2 = $stmt->fetch(PDO::FETCH_ASSOC); var_dump($res);
var_dump($res2);
输出:
array
'id' => string '1' (length=1)
'content' => string 'hello' (length=5)
'flag' => string '1' (length=1)
array
'id' => string '2' (length=1)
'content' => string 'world' (length=5)
'flag' => string '1' (length=1)
插入一行数据并获取 ID
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'insert into message (content, flag) values (:content, :flag)';
$stmt = $db_conn->prepare($sql); $stmt->execute(array(
':content'=>'诸葛四郎和魔鬼党,到底谁抢到那支宝剑',
':flag'=>2
)
); echo 'new id: ',$db_conn->lastInsertId();
输出:new id: 3
说明:lastinsertId() 是 PDO 对象而不是 PDOStatement 对象。
注意:插入中文时为避免乱码,需要在实例化 PDO 对象时增加一个参数 $opt:
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = '';
$opt = array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"); try {
$db_conn = new PDO($dsn, $username, $pwd, $opt);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
}
返回插入、更新或删除的数据的数量
当执行 Insert、Update、Delete 语句时,可以通过 rowCount() 方法 找出多少行内容已经改变:
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'update message set flag = :flag where flag = 1';
$stmt = $db_conn->prepare($sql); $stmt->execute(array(':flag'=>3)); echo $stmt->rowCount(),' rows uodated.';
输出:2 rows uodated.
说明:rowCount() 方法是 PDOStatement 对象。
删除数据
<?php
$dsn = 'mysql:host=localhost;dbname=msg';
$username = 'root';
$pwd = ''; try {
$db_conn = new PDO($dsn, $username, $pwd);
}catch (PDOException $e) {
echo 'Could not connect to database.<br />'.$e->getMessage();
} $sql = 'delete from message where id = :msgid';
$stmt = $db_conn->prepare($sql); $stmt->execute(array(':msgid'=>2)); echo $stmt->rowCount(),' row(s) deleted.';
输出:1 row(s) deleted.
附:《PDO 学习与使用 ( 一 ) :PDO 对象、exec 方法、query 方法与防 SQL 注入》
PDO 学习与使用 ( 二 ) PDO 数据提取 和 预处理语句的更多相关文章
- PHP中的MySQLi扩展学习(四)mysqli的事务与预处理语句
对于 MySQLi 来说,事务和预处理语句当然是它之所以能够淘汰 MySQL(原始) 扩展的资本.我们之前也已经学习过了 PDO 中关于事务和预处理语句相关的内容.所以在这里,我们就不再多讲理论方面的 ...
- Scrapy 学习笔记(一)数据提取
Scrapy 中常用的数据提取方式有三种:Css 选择器.XPath.正则表达式. Css 选择器 Web 中的 Css 选择器,本来是用于实现在特定 DOM 元素上应用花括号内的样式这样一个功能的. ...
- MySQL学习笔记十二:数据备份与恢复
数据备份 1.物理备份与逻辑备份 物理备份 物理备份就是将数据库的数据文件,配置文件,日志文件等复制一份到其他路径上,这种备份速度一般较快,因为只有I/O操作.进行物理备份时,一般都需要关闭mysql ...
- C Primer Plus学习笔记(二)- 数据和C
从一个简单的程序开始 #include <stdio.h> int main(void) { float weight; float value; printf("Please ...
- 使用Tensorflow搭建回归预测模型之二:数据准备与预处理
前言: 在前一篇中,已经搭建好了Tensorflow环境,本文将介绍如何准备数据与预处理数据. 正文: 在机器学习中,数据是非常关键的一个环节,在模型训练前对数据进行准备也预处理是非常必要的. 一.数 ...
- jmeter之断言、数据提取器(正则表达式、jsonpath、beanshell)、聚合报告、参数化
ctx - ( JMeterContext) - gives access to the context vars - ( JMeterVariables) - gives read/write ac ...
- PHP中的PDO操作学习(二)预处理语句及事务
今天这篇文章,我们来简单的学习一下 PDO 中的预处理语句以及事务的使用,它们都是在 PDO 对象下的操作,而且并不复杂,简单的应用都能很容易地实现.只不过大部分情况下,大家都在使用框架,手写的机会非 ...
- Java学习-028-JSON 之二 -- 数据读取
JSON数据由 JSONObject.JSONArray.key_value 组合而成.通常来说,JSONObject 可以包含 JSONObject.JSONArray.key_value:JSON ...
- 【转】Pandas学习笔记(二)选择数据
Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...
随机推荐
- 何时使用hadoop fs、hadoop dfs与hdfs dfs命令(转)
hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件系统相关(包括与Local FS间的操作),前者已经Deprecated,一般使用后 ...
- 使用rsync 的 --delete参数删除目标目录比源目录多余的文件
root@v01 ~]# mkdir dir01 dir02 [root@v01 ~]# ls anaconda-ks.cfg dir02 framework install.log.syslog m ...
- Xamarin.Android开发实践(十七)
Xamarin.Android之定位 一.前言 打开我们手中的应用,可以发现越来越多的应用使用了定位,从而使我们的生活更加方便,所以本章我们将学习如何在Xamarin中进行定位的开发. 二.准备工作 ...
- [杂]SQL Server 之 Understanding Connection Pooling and Transactions
A SqlConnection consists of two parts: the public instance that your code interacts with (the outer ...
- poj 2923 状压dp+01背包
好牛b的思路 题意:一系列物品,用二辆车运送,求运送完所需的最小次数,两辆车必须一起走 解法为状态压缩DP+背包,本题的解题思路是先枚举选择若干个时的状态,总状态量为1<<n,判断这些状态 ...
- hdu 2795 线段树(纵向)
注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...
- float数据在内存中是怎么存储的 AND IEEE754测试程序
float类型数字在计算机中用4个字节存储.遵循IEEE-754格式标准: 一个浮点数有2部分组成:底数m和指数e 底数部分 使用二进制数来表示此浮点数的实际值指数部分 占用8bit的二进制数,可表示 ...
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- Square Coins[HDU1398]
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- APIO2015
还没有写完APIO2015的题目,打算今天写一写. T1: 按位DP,DP时要保证已确定的位为0. 前4组设f[n][k]表示把前n个分成k组是否合法. 最后一组设g[n]表示把前n个最少分为多少组才 ...