• PHP Video Tutorials

PHP mysqli_stmt_data_seek() Function



Definition and Usage

The function accepts a statement object and an integer value as parameters and seeks to the specified row in the result set of the given statement (if any). Make sure that you have stored the result set (using mysqli_stmt_data_seek()) before invoking this function.

Syntax

mysqli_stmt_data_seek($stmt);

Parameters

Sr.No Parameter & Description
1

stmt(Mandatory)

This is an object representing a prepared statement.

2

offset(Mandatory)

This is an integer value representing the desired row (must be between 0 and the total number of rows in the result set).

Return Values

The PHP mysqli_stmt_data_seek() function returns doesnot return any value.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_stmt_data_seek() function (in procedural style) −

<?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);
?>

This will produce following result −

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

Example

In object oriented style the syntax of this function is $stmt->data_seek(); Following is the example of this function in object oriented style $minus;

<?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();
?>

This will produce following result −

Table Created.....
Name: Sarmista
Age: 27
php_function_reference.htm
Advertisements