mysqli_stmt::execute
(PHP 5, PHP 7, PHP 8)
mysqli_stmt::execute -- mysqli_stmt_execute - 준비된 명령문 실행
설명
객체 지향 스타일
public mysqli_stmt::execute(?array $params
= null
): bool
절차적 스타일
mysqli_stmt_execute(mysqli_stmt $statement
, ?array $params
= null
): bool
이전에 준비된 명령문을 실행합니다. 명령문은 mysqli_prepare() 또는 mysqli_stmt_prepare() 함수를 사용하거나 mysqli_stmt::__construct()에 두 번째 인수를 전달하여 실행 전에 성공적으로 준비되어야 합니다.
명령문이 UPDATE
, DELETE
또는 INSERT
인 경우 영향을 받는 행의 총 수는 mysqli_stmt_affected_rows() 함수를 사용하여 결정할 수 있습니다. 쿼리가 결과 세트를 생성하면 mysqli_stmt_get_result() 함수를 사용하거나 mysqli_stmt_fetch() 함수를 사용하여 명령문에서 직접 행별로 페치하여 가져올 수 있습니다.
매개변수
statement
- 절차적 스타일 전용: mysqli_stmt_init()에 의해 반환된 mysqli_stmt 객체.
params
- 실행 중인 SQL 문에 바인딩된 매개 변수가 있는 만큼 요소가 있는 선택적 목록 배열입니다. 각 값은 문자열로 처리됩니다.
반환 값
성공하면 true
를, 실패하면 false
를 반환합니다.
변경 로그
버전 | 설명 |
---|---|
8.1.0 | 선택적 params 매개 변수가 추가되었습니다. |
Examples
예제 #1 바인딩된 변수로 준비된 명령문 실행
객체 지향 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");
/* Prepare an insert statement */
$stmt = $mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");
/* Bind variables to parameters */
$stmt->bind_param("sss", $val1, $val2, $val3);
$val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';
/* Execute the statement */
$stmt->execute();
$val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';
/* Execute the statement */
$stmt->execute();
/* Retrieve all rows from myCity */
$query = "SELECT Name, CountryCode, District FROM myCity";
$result = $mysqli->query($query);
while ($row = $result->fetch_row()) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
}
절차적 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");
/* Prepare an insert statement */
$stmt = mysqli_prepare($link, "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");
/* Bind variables to parameters */
mysqli_stmt_bind_param($stmt, "sss", $val1, $val2, $val3);
$val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';
/* Execute the statement */
mysqli_stmt_execute($stmt);
$val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';
/* Execute the statement */
mysqli_stmt_execute($stmt);
/* Retrieve all rows from myCity */
$query = "SELECT Name, CountryCode, District FROM myCity";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result)) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
}
위의 예는 다음을 출력합니다.
Stuttgart (DEU,Baden-Wuerttemberg) Bordeaux (FRA,Aquitaine)
예제 #2 값 배열로 준비된 명령문 실행
객체 지향 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
$mysqli->query('CREATE TEMPORARY TABLE myCity LIKE City');
/* Prepare an insert statement */
$stmt = $mysqli->prepare('INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');
/* Execute the statement */
$stmt->execute(['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);
/* Retrieve all rows from myCity */
$query = 'SELECT Name, CountryCode, District FROM myCity';
$result = $mysqli->query($query);
while ($row = $result->fetch_row()) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
}
절차적 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
mysqli_query($link, 'CREATE TEMPORARY TABLE myCity LIKE City');
/* Prepare an insert statement */
$stmt = mysqli_prepare($link, 'INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');
/* Execute the statement */
mysqli_stmt_execute($stmt, ['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);
/* Retrieve all rows from myCity */
$query = 'SELECT Name, CountryCode, District FROM myCity';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_row($result)) {
printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
}
위의 예는 다음을 출력합니다.
Stuttgart (DEU,Baden-Wuerttemberg)
기타
- mysqli_prepare() - 실행을 위해 SQL 문 준비
- mysqli_stmt_bind_param() - 변수를 준비된 명령문에 매개변수로 바인딩
- mysqli_stmt_get_result() - 준비된 명령문에서 결과 세트를 mysqli_result 객체로 가져옵니다.