mysqli_stmt_fetch()函數(shù)將預(yù)準(zhǔn)備語句中的結(jié)果提取到綁定變量中。
您可以使用mysqli_prepare()函數(shù)創(chuàng)建一個預(yù)處理語句,該語句具有參數(shù)標(biāo)記(“?”)(如果有值)。預(yù)處理語句后,需要使用mysqli_stmt_bind_param()函數(shù)將值綁定到所創(chuàng)建語句的參數(shù)。
以相同的方式,可以使用 mysqli_stmt_bind_result() 函數(shù)將語句的結(jié)果集的列綁定到所需的變量。
如果調(diào)用了mysqli_stmt_fetch()函數(shù),則在綁定列之后,它將結(jié)果的列提取到指定的變量中。
mysqli_stmt_fetch($stmt);
序號 | 參數(shù)及說明 |
---|---|
1 | stmt(必需) 這是表示準(zhǔn)備好的語句的對象。 |
如果獲取數(shù)據(jù),PHP mysqli_stmt_fetch()函數(shù)將返回TRUE;如果出現(xiàn)錯誤,則返回FALSE;如果結(jié)果中沒有更多行,則返回NULL。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_fetch()函數(shù)的用法(面向過程風(fēng)格),使用預(yù)處理語句將結(jié)果中的值綁定到變量:
<?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')"); print("插入記錄.....\n"); //檢索表的內(nèi)容 $stmt = mysqli_prepare($con, "SELECT * FROM myplayers"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //將結(jié)果中的值綁定到變量 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"); } //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... Id: 1 fname: Sikhar lname: Dhawan pob: Delhi country: India Id: 2 fname: Jonathan lname: Trott pob: CapeTown country: SouthAfrica
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$stmt->fetch();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例,使用預(yù)處理語句將變量綁定到結(jié)果集:
<?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("記錄已刪除.....\n"); //執(zhí)行語句 $stmt->execute(); //將變量綁定到結(jié)果集 $stmt->bind_result($name, $age); while ($stmt->fetch()) { print("Name: ".$name."\n"); print("Age: ".$age."\n"); } //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... 記錄已刪除..... Name: Raju Age: 25 Name: Rahman Age: 30
以下示例使用 mysqli_stmt_bind_result() 和 mysqli_stmt_fetch() 函數(shù)獲取DESCRIBE查詢的結(jié)果-
<?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"); //表的說明 $stmt = mysqli_prepare($con, "DESC myplayers"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //將結(jié)果中的值綁定到變量 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"); } //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(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查詢的結(jié)果,返回當(dāng)前數(shù)據(jù)庫中的所有表:
<?php $con = mysqli_connect("localhost", "root", "password"); //選擇數(shù)據(jù)庫 mysqli_query($con, "CREATE DATABASE NewDatabase"); mysqli_select_db($con, "NewDatabase"); //創(chuàng)建表 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("創(chuàng)建的表.....\n"); //顯示表 $stmt = mysqli_prepare($con, "SHOW TABLES"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //將結(jié)果中的值綁定到變量 mysqli_stmt_bind_result($stmt, $table_name); print("當(dāng)前數(shù)據(jù)庫中的所有表: \n"); while (mysqli_stmt_fetch($stmt)) { print($table_name."\n"); } //結(jié)束語句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建的表..... 當(dāng)前數(shù)據(jù)庫中的所有表: test1 test2 test3