mysqli_stmt_bind_result()函數(shù)將變量綁定到預處理語句以存儲結果
mysqli_stmt_bind_result()函數(shù)用于將結果集的列綁定到變量。綁定變量后,您需要調用 mysqli_stmt_fetch() 函數(shù)以獲取指定變量中的列的值。
mysqli_stmt_bind_result($stmt, $var1, $var2...);
序號 | 參數(shù)及說明 |
---|---|
1 | stmt(必需) 這是表示準備好的語句的對象。 |
2 | var1(必需) 這表示要綁定到列的變量。 |
PHP mysqli_stmt_bind_result()函數(shù)返回一個布爾值,成功時為true,失敗時為false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_bind_result()函數(shù)的用法(面向過程風格),將結果綁定到變量:
<?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("創(chuàng)建表.....\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"); //執(zhí)行語句 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); ?>
輸出結果
創(chuàng)建表..... 插入記錄..... 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
在面向對象風格中,此函數(shù)的語法為$stmt-> bind_result();。以下是面向對象風格中此函數(shù)的示例,將變量綁定到結果集:
<?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("創(chuàng)建表.....\n"); $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; print("Records Deleted.....\n"); //執(zhí)行語句 $stmt->execute(); //將變量綁定到結果集 $stmt->bind_result($name, $age); while ($stmt->fetch()) { print("Name: ".$name."\n"); print("Age: ".$age."\n"); } //結束語句 $stmt->close(); //關閉連接 $con->close(); ?>
輸出結果
創(chuàng)建表..... Records Deleted..... Name: Raju Age: 25 Name: Rahman Age: 30
以下示例使用 mysqli_stmt_bind_result() 和 mysqli_stmt_fetch() 函數(shù)獲取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("創(chuàng)建表.....\n"); //Description of the table $stmt = mysqli_prepare($con, "DESC myplayers"); //執(zhí)行語句 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); ?>
輸出結果
創(chuàng)建表..... 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() 函數(shù)獲取SHOW TABLES查詢的結果,列出數(shù)據(jù)庫中所有的數(shù)據(jù)表:
<?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"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //將結果中的值綁定到變量 mysqli_stmt_bind_result($stmt, $table_name); print("當前數(shù)據(jù)庫中所有的表如下: \n"); while (mysqli_stmt_fetch($stmt)) { print($table_name."\n"); } //結束語句 mysqli_stmt_close($stmt); //關閉連接 mysqli_close($con); ?>
輸出結果
Tables Created..... 當前數(shù)據(jù)庫中所有的表如下: test1 test2 test3