mysqli_stmt::bind_param
mysqli_stmt::bind_param
mysqli_stmt_bind_param
(PHP 5, PHP 7)
mysqli_stmt :: bind_param - mysqli_stmt_bind_param - 将变量作为参数绑定到预处理语句
描述
面向对象的风格
bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
程序风格
bool mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )
绑定传递给mysqli_prepare()的SQL语句中参数标记的变量。
注意
:如果变量的数据大小超过最大值。允许的数据包大小(max_allowed_packet),您必须指定b
intypes
并使用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()返回的语句标识符。
types
一个字符串,其中包含一个或多个指定相应绑定变量类型的字符:
字符 | 描述 |
---|---|
一世 | 相应的变量有整型 |
d | 相应的变量有double类型 |
小号 | 相应的变量有字符串类型 |
b | 相应的变量是一个blob,并将以包的形式发送 |
var1
变量的数量和字符串的长度types
必须与语句中的参数相匹配。
返回值
返回TRUE
成功或返回FALSE
失败。
例子
Example #1 Object oriented style
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world'
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"
$stmt->bind_param('sssd', $code, $language, $official, $percent
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
$stmt->execute(
printf("%d Row inserted.\n", $stmt->affected_rows
/* close statement and connection */
$stmt->close(
/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'"
printf("%d Row deleted.\n", $mysqli->affected_rows
/* close connection */
$mysqli->close(
?>
Example #2 Procedural style
<?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
?>
上面的例子会输出:
1 Row inserted.
1 Row deleted.