PHP | mysqli_stmt_data_seek() Function

PHP  mysqli_stmt_data_seek() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_stmt_data_seek() Function.

The built-in PHP mysqli_stmt_data_seek() 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).

Syntax

Following below is the syntax to use this function -

mysqli_stmt_data_seek($stmt);


Parameter Details

Sr.NoParameter & 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 Value

This built-in PHP function does not return any value.

PHP Version

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

Example1

The following below is an example which demonstrates the usage of the built-in PHP mysqli_stmt_data_seek() function (in a 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, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Queen', 'Douglas', 'Texas', 'UnitedStates')");
   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);
?>

Output

When the above code is executed, it will produce the following result -

Table Created.....
Record Inserted.....
Id: 3
fname: Queen
lname: Douglas
pob: Texas
country: UnitedStates

Example2

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

<?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('Kennedy', 27),('Paul', 30),('Justice', 28)");
   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();
?>

Output

When the above code is executed, it will produce the following result -

Table Created.....
Name: Justice
Age: 28


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the mysqli_stmt_errno() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.