PHP mysqli_stmt_data_seek() 函式



定義和用法

該函式接受一個語句物件和一個整數值作為引數,並在給定語句的結果集中(如果有)搜尋指定的行。請確保在呼叫此函式之前已儲存了結果集(使用 mysqli_stmt_data_seek())。

語法

mysqli_stmt_data_seek($stmt);

引數

序號 引數及描述
1

stmt(必填)

這是一個表示預處理語句的物件。

2

offset(必填)

這是一個整數值,表示所需的行的偏移量(必須在 0 到結果集中行的總數之間)。

返回值

PHP mysqli_stmt_data_seek() 函式不返回任何值。

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

以下示例演示了 mysqli_stmt_data_seek() 函式(以過程式風格)的用法:

<?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("Table Created.....\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("Record Inserted.....\n");

   //Retrieving the contents of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Binding values in result to variables
   mysqli_stmt_bind_result($stmt, $id, $fname, $lname, $pob, $country);

   //Storing the result
   mysqli_stmt_store_result($stmt);

   //Moving the seek
   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");

   //Closing the statement
   mysqli_stmt_close($stmt);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果:

Table Created.....
Record Inserted.....
Id: 3
fname: Kumara
lname: Sangakkara
pob: Matale
country: Srilanka

示例

在面向物件風格中,此函式的語法為 $stmt->data_seek(); 以下是此函式在面向物件風格中的示例:

<?php
   //Creating a connection
   $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("Table Created.....\n");
   $stmt = $con -> prepare( "SELECT * FROM Test");

   //Executing the statement
   $stmt->execute();

   //Binding variables to resultset
   $stmt->bind_result($name, $age);

   $stmt->store_result();

   //Moving the seek
   $stmt->data_seek(2);

   $stmt->fetch();
   print("Name: ".$name."\n");
   print("Age: ".$age."\n");

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

這將產生以下結果:

Table Created.....
Name: Sarmista
Age: 27
php_function_reference.htm
廣告