mysqli_stmt_data_seek()函數(shù)查找語句結(jié)果集中的任意行。
該函數(shù)接受一個語句對象和一個整數(shù)值作為參數(shù),并在給定語句的結(jié)果集中查找指定的行(如果有)。調(diào)用此函數(shù)(使用mysqli_stmt_data_seek())之前,請確保已存儲了結(jié)果集。
mysqli_stmt_data_seek($stmt);
序號 | 參數(shù)及說明 |
---|---|
1 | stmt(必需) 這是表示準(zhǔn)備好的語句的對象。 |
2 | offset(必需) 這是表示所需行的整數(shù)值(必須在0到結(jié)果集中的行總數(shù)之間)。 |
PHP mysqli_stmt_data_seek()函數(shù)返回不返回任何值。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_data_seek()函數(shù)的用法(面向過程風(fēng)格)-
<?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"); //檢索表的內(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); //存儲結(jié)果 mysqli_stmt_store_result($stmt); //移動搜索 mysqli_stmt_data_seek($stmt, 2); 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: 3 fname: Kumara lname: Sangakkara pob: Matale country: Srilanka
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$stmt-> data_seek();。以下是面向?qū)ο箫L(fēng)格中此函數(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"); //執(zhí)行語句 $stmt->execute(); //將變量綁定到結(jié)果集 $stmt->bind_result($name, $age); $stmt->store_result(); //移動搜索 $stmt->data_seek(2); $stmt->fetch(); print("Name: ".$name."\n"); print("Age: ".$age."\n"); //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... Name: Sarmista Age: 27