mysqli_stmt::bind_result
mysqli_stmt::bind_result
mysqli_stmt_bind_result
(PHP 5, PHP 7)
mysqli_stmt :: bind_result - mysqli_stmt_bind_result - 将变量绑定到预处理结果存储的语句
描述
面向对象的风格
bool mysqli_stmt::bind_result ( mixed &$var1 [, mixed &$... ] )
程序风格
bool mysqli_stmt_bind_result ( mysqli_stmt $stmt , mixed &$var1 [, mixed &$... ] )
将结果集中的列绑定到变量。
当调用mysqli_stmt_fetch()来获取数据时,MySQL客户端/服务器协议将绑定列的数据放入指定的变量中var1, ...
。
注意
:请注意
,在调用mysqli_stmt_fetch()之前,必须在mysqli_stmt_execute()之后绑定所有列。根据列类型,绑定变量可以默默地变为相应的PHP类型。即使在部分检索结果集之后,也可以随时绑定或反弹列。下一次调用mysqli_stmt_fetch()时,新绑定生效。
参数
`stmt`
仅过程风格:由mysqli_stmt_init()返回的语句标识符。
var1
要绑定的变量。
返回值
TRUE
成功或FALSE
失败时返回。
例子
Example #1 Object oriented style
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
$stmt->execute(
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2
/* fetch values */
while ($stmt->fetch()) {
printf("%s %s\n", $col1, $col2
}
/* close statement */
$stmt->close(
}
/* 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(
}
/* prepare statement */
if ($stmt = mysqli_prepare($link, "SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
mysqli_stmt_execute($stmt
/* bind variables to prepared statement */
mysqli_stmt_bind_result($stmt, $col1, $col2
/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
printf("%s %s\n", $col1, $col2
}
/* close statement */
mysqli_stmt_close($stmt
}
/* close connection */
mysqli_close($link
?>
上面的例子会输出:
AFG Afghanistan
ALB Albania
DZA Algeria
ASM American Samoa
AND Andorra