PDOStatement::execute
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDOStatement::execute — 준비된 명령문을 실행합니다.
설명
public PDOStatement::execute(?array $params
= null
): bool
준비된 명령문을 실행합니다. 준비된 명령문에 매개변수 표시문자가 포함된 경우 다음 중 하나입니다.
- PDOStatement::bindParam() 및/또는 PDOStatement::bindValue()는 변수 또는 값(각각)을 매개변수 마커에 바인딩하기 위해 호출되어야 합니다. 바운드 변수는 값을 입력으로 전달하고 관련 매개변수 마커의 출력 값(있는 경우)을 받습니다.
- 또는 입력 전용 매개변수 값의 배열을 전달해야 합니다.
매개변수
params
- 실행 중인 SQL 문에 바인딩된 매개 변수 수만큼 요소가 있는 값 배열입니다. 모든 값은 PDO::PARAM_STR로 처리됩니다.
여러 값을 단일 매개변수에 바인딩할 수 없습니다. 예를 들어, IN() 절의 단일 명명된 매개변수에 두 개의 값을 바인딩하는 것은 허용되지 않습니다.
지정된 것보다 많은 값을 바인딩할 수 없습니다. PDO::prepare()에 지정된 SQL보다
params
에 더 많은 키가 있으면 명령문이 실패하고 오류가 발생합니다.
반환 값
성공하면 true
를, 실패하면 false
를 반환합니다.
Examples
예제 #1 바인딩된 변수와 값으로 준비된 명령문 실행
<?php
/* Execute a prepared statement by binding a variable and value */
$calories = 150;
$colour = 'gre';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour LIKE :colour');
$sth->bindParam('calories', $calories, PDO::PARAM_INT);
/* Names can be prefixed with colons ":" too (optional) */
$sth->bindValue(':colour', "%$colour%");
$sth->execute();
?>
예제 #2 명명된 값의 배열로 준비된 명령문 실행
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->execute(array('calories' => $calories, 'colour' => $colour));
/* Array keys can be prefixed with colons ":" too (optional) */
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>
예제 #3 위치 값 배열로 준비된 명령문 실행
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?>
예제 #4 위치 자리 표시자에 바인딩된 변수를 사용하여 준비된 명령문 실행
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
예제 #5 IN 절에 대한 배열을 사용하여 준비된 명령문 실행
<?php
/* Execute a prepared statement using an array of values for an IN clause */
$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));
/*
This prepares the statement with enough unnamed placeholders for every value
in our $params array. The values of the $params array are then bound to the
placeholders in the prepared statement when the statement is executed.
This is not the same thing as using PDOStatement::bindParam() since this
requires a reference to the variable. PDOStatement::execute() only binds
by value instead.
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>
노트
메모: 일부 드라이버는 다음 명령문을 실행하기 전에 커서를 닫아야 합니다.
기타
- PDO::prepare() - 실행할 명령문을 준비하고 명령문 개체를 반환합니다.
- PDOStatement::bindParam() - 매개변수를 지정된 변수 이름에 바인딩합니다.
- PDOStatement::fetch() - 결과 집합에서 다음 행을 가져옵니다.
- PDOStatement::fetchAll() - 결과 집합에서 나머지 행을 가져옵니다.
- PDOStatement::fetchColumn() - 결과 집합의 다음 행에서 단일 열을 반환합니다.