定义和用法

mysqli_stmt_bind_param – 将变量绑定到准备好的语句作为参数

版本支持

php4 php5 php7
不支持 支持 支持

语法

mysqli_stmt_bind_param ( mysqli_stmt $stmt , string $types , mixed &$var1 [, mixed &$... ] )

在传递给  的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 由  返回的 statement 标识。
types 一个包含一个或多个字符的字符串,这些字符指定相应绑定变量的类型:

  • i – 对应的变量具有整数类型
  • d – 对应变量的类型为double
  • s – 对应的变量具有字符串类型
  • b – 对应的变量是blob,将以数据包形式发送
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);

相关函数

 – 将变量绑定到准备好的语句以存储结果  – 执行准备好的查询  – 从准备好的语句中获取结果到绑定变量中  – 准备执行一个sql语句  – 分块发送数据  – 返回最近的语句调用的错误代码  – 返回最后一条语句错误的字符串描述