mysqli_stmt_bind_result()函数将变量绑定到预处理语句以存储结果
mysqli_stmt_bind_result()函数用于将结果集的列绑定到变量。绑定变量后,您需要调用 mysqli_stmt_fetch() 函数以获取指定变量中的列的值。
mysqli_stmt_bind_result($stmt, $var1, $var2...);
序号 | 参数及说明 |
---|---|
1 | stmt(必需) 这是表示准备好的语句的对象。 |
2 | var1(必需) 这表示要绑定到列的变量。 |
PHP mysqli_stmt_bind_result()函数返回一个布尔值,成功时为true,失败时为false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_bind_result()函数的用法(面向过程风格),将结果绑定到变量:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("创建表.....\n"); mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')"); mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); print("插入记录.....\n"); //检索表的内容 $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); //执行语句 mysqli_stmt_execute($stmt); //将结果中的值绑定到变量 mysqli_stmt_bind_result($stmt, $id, $fname, $lname, $pob, $country); while (mysqli_stmt_fetch($stmt)) { print("Id: ".$id."\n"); print("fname: ".$fname."\n"); print("lname: ".$lname."\n"); print("pob: ".$pob."\n"); print("country: ".$country."\n"); print("\n"); } //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... 插入记录..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: CapeTown country: SouthAfrica Id: 3 fname: Kumara lname: Sangakkara pob: Matale country: Srilanka
在面向对象风格中,此函数的语法为$stmt-> bind_result();。以下是面向对象风格中此函数的示例,将变量绑定到结果集:
<?php //建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("创建表.....\n"); $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; print("Records Deleted.....\n"); //执行语句 $stmt->execute(); //将变量绑定到结果集 $stmt->bind_result($name, $age); while ($stmt->fetch()) { print("Name: ".$name."\n"); print("Age: ".$age."\n"); } //结束语句 $stmt->close(); //关闭连接 $con->close(); ?>
输出结果
创建表..... Records Deleted..... Name: Raju Age: 25 Name: Rahman Age: 30
以下示例使用 mysqli_stmt_bind_result() 和 mysqli_stmt_fetch() 函数获取DESCRIBE查询的结果,并将结果中的值绑定到变量:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("创建表.....\n"); //Description of the table $stmt = mysqli_prepare($con, "DESC myplayers"); //执行语句 mysqli_stmt_execute($stmt); //将结果中的值绑定到变量 mysqli_stmt_bind_result($stmt, $field, $type, $null, $key, $default, $extra); while (mysqli_stmt_fetch($stmt)) { print("Field: ".$field."\n"); print("Type: ".$type."\n"); print("Null: ".$null."\n"); print("Key: ".$key."\n"); print("Default: ".$default."\n"); print("Extra: ".$extra."\n"); print("\n"); } //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
创建表..... Field: ID Type: int(11) Null: YES Key: Default: Extra: Field: First_Name Type: varchar(255) Null: YES Key: Default: Extra: Field: Last_Name Type: varchar(255) Null: YES Key: Default: Extra: Field: Place_Of_Birth Type: varchar(255) Null: YES Key: Default: Extra: Field: Country Type: varchar(255) Null: YES Key: Default: Extra:
以下示例使用 mysqli_stmt_bind_result() 和 mysqli_stmt_fetch() 函数获取SHOW TABLES查询的结果,列出数据库中所有的数据表:
<?php $con = mysqli_connect("localhost", "root", "password"); //Selecting the database mysqli_query($con, "CREATE DATABASE NewDatabase"); mysqli_select_db($con, "NewDatabase"); //Creating tables mysqli_query($con, "CREATE TABLE test1(Name VARCHAR(255), Age INT)"); mysqli_query($con, "CREATE TABLE test2(Name VARCHAR(255), Age INT)"); mysqli_query($con, "CREATE TABLE test3(Name VARCHAR(255), Age INT)"); print("Tables Created.....\n"); //表的说明 $stmt = mysqli_prepare($con, "SHOW TABLES"); //执行语句 mysqli_stmt_execute($stmt); //将结果中的值绑定到变量 mysqli_stmt_bind_result($stmt, $table_name); print("当前数据库中所有的表如下: \n"); while (mysqli_stmt_fetch($stmt)) { print($table_name."\n"); } //结束语句 mysqli_stmt_close($stmt); //关闭连接 mysqli_close($con); ?>
输出结果
Tables Created..... 当前数据库中所有的表如下: test1 test2 test3