mysqli_stmt::bind_param

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::bind_param -- mysqli_stmt_bind_param — 변수를 매개변수로 준비된 명령문에 바인딩


설명

객체 지향 스타일

public mysqli_stmt::bind_param(string $types, mixed &$var, mixed &...$vars): bool

절차적 스타일

mysqli_stmt_bind_param(
    mysqli_stmt $statement,
    string $types,
    mixed &$var,
    mixed &...$vars
): bool
                

mysqli_prepare() 또는 mysqli_stmt_prepare()에 의해 준비된 SQL 문의 매개변수 마커에 대한 바인드 변수.

메모: 변수의 데이터 크기가 최대값을 초과하는 경우 허용된 패킷 크기(max_allowed_packet), types에 b를 지정하고 mysqli_stmt_send_long_data()를 사용하여 데이터를 패킷으로 보내야 합니다.

메모: call_user_func_array()와 함께 mysqli_stmt_bind_param()을 사용할 때 주의해야 합니다. mysqli_stmt_bind_param()은 참조로 전달되는 매개변수를 요구하는 반면 call_user_func_array()는 참조 또는 값을 나타낼 수 있는 변수 목록을 매개변수로 받아들일 수 있습니다.


매개변수

statement
절차적 스타일 전용: mysqli_stmt_init()에 의해 반환된 mysqli_stmt 객체.
types
해당 바인드 변수의 유형을 지정하는 하나 이상의 문자를 포함하는 문자열:

유형 사양 문자

Character 설명
i 해당 변수에는 정수 유형이 있습니다.
d 해당 변수의 유형은 double입니다.
s 해당 변수에는 문자열 유형이 있습니다.
b 해당 변수는 blob이며 패킷으로 전송됩니다.
var
vars
변수의 수와 문자열 types의 길이는 명령문의 매개변수와 일치해야 합니다.

반환 값

성공하면 true를, 실패하면 false를 반환합니다.


Examples

예제 #1 mysqli_stmt::bind_param() 예제

객체 지향 스타일

                  
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

$stmt->execute();

printf("%d row inserted.\n", $stmt->affected_rows);

/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d row deleted.\n", $mysqli->affected_rows);
                  
                

절차적 스타일

                  
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

mysqli_stmt_execute($stmt);

printf("%d row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d row deleted.\n", mysqli_affected_rows($link));
                  
                

위의 예는 다음을 출력합니다.

1 row inserted.
1 row deleted.
                

예제 #2 ...를 사용하여 인수 제공

... 연산자는 가변 길이 인수 목록을 제공하는 데 사용할 수 있습니다. WHERE IN 절에서.

                  
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

$stmt = $mysqli->prepare("SELECT Language FROM CountryLanguage WHERE CountryCode IN (?, ?)");
/* Using ... to provide arguments */
$stmt->bind_param('ss', ...['DEU', 'POL']);
$stmt->execute();
$stmt->store_result();

printf("%d rows found.\n", $stmt->num_rows());
                  
                

위의 예는 다음을 출력합니다.

10 rows found.
                

기타