定義和用法
mysqli_stmt_bind_param - 將變量綁定到准備好的語句作為參數
版本支持
| PHP4 | PHP5 | PHP7 |
|---|---|---|
| 不支持 | 支持 | 支持 |
語法
mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )
在傳遞給 mysqli_prepare() 的SQL語句中為參數標記綁定變量。
注意: 如果變量的數據大小超過最大值。 如果允許數據包大小(max_allowed_packet),則必須在類型中指定b並使用 mysqli_stmt_send_long_data()以數據包形式發送數據。
注意: 將 mysqli_stmt_bind_param()與 call_user_func_array()結合使用時必須小心。請注意, mysqli_stmt_bind_param()要求參數通過引用傳遞,而 call_user_func_array()可以接受可以表示引用或值的變量列表作為參數。
參數
| 參數 | 必需的 | 描述 |
|---|---|---|
| stmt | 是 | 由 mysqli_stmt_init() 返回的 statement 標識。 |
| types | 是 | 一個包含一個或多個字符的字符串,這些字符指定相應綁定變量的類型:
|
| var1 | 是 | 變量的數量和字符串類型的長度必須與語句中的參數匹配。 |
| ... | 否,取決types,是否有變量需要綁定 | 更多變量 |
返回值
成功時返回 TRUE, 或者在失敗時返回 FALSE。
示例
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$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;
/* execute prepared statement */
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
/* close statement and connection */
mysqli_stmt_close($stmt);
/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", mysqli_affected_rows($link));
/* close connection */
mysqli_close($link);
相關函數
mysqli_stmt_bind_result() - 將變量綁定到准備好的語句以存儲結果
mysqli_stmt_execute() - 執行准備好的查詢
mysqli_stmt_fetch() - 從准備好的語句中獲取結果到綁定變量中
mysqli_prepare() - 准備執行一個SQL語句
mysqli_stmt_send_long_data() - 分塊發送數據
mysqli_stmt_errno() - 返回最近的語句調用的錯誤代碼
mysqli_stmt_error() - 返回最后一條語句錯誤的字符串描述
