Home  >  Article  >  Backend Development  >  PDOStatement::execute语句有什么用

PDOStatement::execute语句有什么用

PHPz
PHPzOriginal
2016-06-06 20:50:191246browse

【PDOStatement::execute】语句用于执行一条预处理语句,其语法是【PDOStatement::execute ([ array $input_parameters ] ) : bool】,表示执行预处理语句。

PDOStatement::execute语句有什么用

PDOStatement::execute有什么用?

PDOStatement::execute — 执行一条预处理语句

说明 

PDOStatement::execute ([ array $input_parameters ] ) : bool

执行预处理过的语句。如果预处理过的语句含有参数标记,必须选择下面其中一种做法:

调用 PDOStatement::bindParam() 绑定 PHP 变量到参数标记:如果有的话,通过关联参数标记绑定的变量来传递输入值和取得输出值或传递一个只作为输入参数值的数组

参数 

input_parameters

一个元素个数和将被执行的 SQL 语句中绑定的参数一样多的数组。所有的值作为 PDO::PARAM_STR 对待。

不能绑定多个值到一个单独的参数;比如,不能绑定两个值到 IN()子句中一个单独的命名参数。

绑定的值不能超过指定的个数。如果在 input_parameters 中存在比 PDO::prepare() 预处理的SQL 指定的多的键名,则此语句将会失败并发出一个错误。

返回值 

成功时返回 TRUE, 或者在失败时返回 FALSE。

更新日志 

版本 

5.2.0 input_parameters 中的键名必须和 SQL 中声明的相匹配。PHP 5.2.0 之前默认忽略。

范例 

Example #1 执行一条绑定变量的预处理语句

<?php
/*  通过绑定 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = &#39;red&#39;;
$sth = $dbh->prepare(&#39;SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour&#39;);
$sth->bindParam(&#39;:calories&#39;, $calories, PDO::PARAM_INT);
$sth->bindParam(&#39;:colour&#39;, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Example #2 使用一个含有插入值的数组执行一条预处理语句(命名参数)

<?php
/* 通过传递一个含有插入值的数组执行一条预处理语句 */
$calories = 150;
$colour = &#39;red&#39;;
$sth = $dbh->prepare(&#39;SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour&#39;);
$sth->execute(array(&#39;:calories&#39; => $calories, &#39;:colour&#39; => $colour));
?>

Example #3 使用一个含有插入值的数组执行一条预处理语句(占位符)

<?php
/*  通过传递一个插入值的数组执行一条预处理语句 */
$calories = 150;
$colour = &#39;red&#39;;
$sth = $dbh->prepare(&#39;SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?&#39;);
$sth->execute(array($calories, $colour));
?>

Example #4 执行一条问号占位符的预处理语句

<?php
/* 通过绑定 PHP 变量执行一条预处理语句 */
$calories = 150;
$colour = &#39;red&#39;;
$sth = $dbh->prepare(&#39;SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?&#39;);
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Example #5 使用数组执行一条含有 IN 子句的预处理语句

<?php
/*  使用一个数组的值执行一条含有 IN 子句的预处理语句 */
$params = array(1, 21, 63, 171);
/*  创建一个填充了和params相同数量占位符的字符串 */
$place_holders = implode(&#39;,&#39;, array_fill(0, count($params), &#39;?&#39;));
/*
    对于 $params 数组中的每个值,要预处理的语句包含足够的未命名占位符 。
    语句被执行时, $params 数组中的值被绑定到预处理语句中的占位符。
    这和使用 PDOStatement::bindParam() 不一样,因为它需要一个引用变量。
    PDOStatement::execute() 仅作为通过值绑定的替代。
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>

注释 

Note:

有些驱动在执行下一条语句前需要 关闭游标 。

更多相关技术文章,请访问PHP中文网

Statement:
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