php prepare的用法是什么

发布时间:2022-05-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php prepare的用法是什么脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

在Php中“PDO::PRepare”表示准备要执行的语句,并返回语句对象,其使用语法如“public PDO::prepare(string $statement, array $driver_options = array())”。

php prepare的用法是什么

本文操作环境:Windows7系统、PHP7.1版、DELL G3

php prepare的用法是什么

PDO::prepare

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PHP 8,PECL pdo >= 0.1.0)

PDO::prepare — 准备要执行的语句,并返回语句对象

说明

public PDO::prepare(string $statement, array $driver_options = array()): PDOStatement

为 PDOStatement::execute() 方法准备待执行的 SQL 语句。 语句模板可以包含零个或多个参数占位标记,格式是命名(:name)或问号(?)的形式,当它执行时将用真实数据取代。 在同一个语句模板里,命名形式和问号形式不能同时使用;只能选择其中一种参数形式。 请用参数形式绑定用户输入的数据,不要直接字符串拼接到查询里。

调用 PDOStatement::execute() 时,每一个值的参数占位标记,名称必须唯一。 除非启用模拟(emulation)模式,同一个语句里无法使用重名的参数。

注意:

参数占位符仅能字面上展示完整的数据。不能是字面的一部分,不能是关键词,不能是标识符,不能是其他任意的范围。 举例说明:不能把多个值绑到单个参数里,然后在 SQL 语句里用 IN() 查询。

如果用不同参数,通过 PDO::prepare() 和 PDOStatement::execute() 多次调用同一个 SQL 语句,将提升应用的性能 —— 驱动可以让客户端/服务器缓存查询和元信息。 同时,调用 PDO::prepare() 和 PDOStatement::execute() 还能阻止 SQL 注入攻击,不需要给参数手动加引号与转义。

如果内置驱动不支持参数,PDO 将模拟出参数的功能;如果驱动仅仅支持其中一种风格(命名参数和问号参数两种),也会自动重写到另外一种风格。

注意: The parser used for emulated prepared statements and for rewrITing named or question mark style parameters supports the non standard backslash escapes for single- and double quotes. That means that terminating quotes immediately preceeded by a backslash are not recognized as such, which may result in wrong detection of parameters causing the prepared statement to fail when it is executed. A work-around is to not use emulated prepares for such SQL queries, and to avoid rewriting of parameters by using a parameter style which is natively supported by the driver.

参数

statement

必须是对目标数据库服务器有效的 SQL 语句模板。

driver_options

数组包含一个或多个 key=>value 键值对,为返回的 PDOStatement 对象设置属性。 常见用法是:设置 PDO::ATTR_CURSOR 为 PDO::CURSOR_SCROLL,将得到可滚动的光标。 某些驱动有驱动级的选项,在 prepare 时就设置。

返回值

如果数据库服务器完成准备了语句, PDO::prepare() 返回 PDOStatement 对象。 如果数据库服务器无法准备语句, PDO::prepare() 返回 false 或抛出 PDOException (取决于 错误处理器)。

注意:

模拟模式下的 prepare 语句不会和数据库服务器交互,所以 PDO::prepare() 不会检查语句。

范例

示例 #1 命名参数形式的 SQL 语句模板

<?php
/* 传入数组的值,并执行准备好的语句 */
$sql = 'SELECT name, colour, calories
    From fruit
    WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
$sth->execute(array(':calories' => 175, ':colour' => 'yellow'));
$yellow = $sth->fetchAll();
?>

示例 #2 问号形式的 SQL 语句模板

<?php
/* 传入数组的值,并执行准备好的语句 */
$sth = $dbh->prepare('SELECT name, colour, calories
    From fruit
    WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
?>

推荐学习:《PHP视频教程》

以上就是php prepare的用法是什么的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的php prepare的用法是什么全部内容,希望文章能够帮你解决php prepare的用法是什么所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:phpprepare