验证两种prepare模式
- 服务端prepare模式( ATTR_EMULATE_PREPARES = false)
- <?php
- $dbms='mysql'; //数据库类型
- $host='xxx'; //数据库主机名
- $dbName='test'; //使用的数据库
- $user='xx'; //数据库连接用户名
- $pass='123456'; //对应的密码
- $dsn="$dbms:host=$host;dbname=$dbName";
- try {
- $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象
- $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
- echo "----- prepare begin -----
- ";
- $stmt = $pdo->prepare("select * from test.chanpin where id = ?");
- echo "----- prepare after -----
- ";
- $stmt->execute([333333]);
- echo "----- execute after -----
- ";
- $rs = $stmt->fetchAll();
- } catch (PDOException $e) {
- die ("Error!: " . $e->getMessage() . "<br/>");
- }
strace -s200 -f php mysql1.php 跟踪
大家可以看到这个模式下,prepare的时候,是将query+占位符 发送给服务端的:
- 本地prepare模式 (ATTR_EMULATE_PREPARES = true )
- <?php
- $dbms='mysql'; //数据库类型
- $host='xx'; //数据库主机名
- $dbName='test'; //使用的数据库
- $user='xx'; //数据库连接用户名
- $pass='123456'; //对应的密码
- $dsn="$dbms:host=$host;dbname=$dbName";
- try {
- $pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象
- $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,true);
- echo "----- prepare begin -----
- ";
- $stmt = $pdo->prepare("select * from test.chanpin where id = ?");
- echo "----- prepare after -----
- ";
- $stmt->execute([333333]);
- echo "----- execute after -----
- ";
- $rs = $stmt->fetchAll();
- } catch (PDOException $e) {
- die ("Error!: " . $e->getMessage() . "<br/>");
- }
(编辑:ASP站长网)
|