• PHP Video Tutorials

PHP mysqli_stmt_store_result() Function



Definition and Usage

The mysqli_stmt_store_result() function accepts a statement object as a parameter and stores the resultset of the given statement locally, if it executes a SELECT, SHOW or, DESCRIBE statement.

Syntax

mysqli_stmt_store_result($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_attr_get() function returns a boolean value which is TRUE in case of success and, FALSE in case of failure.

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_store_result() function (in procedural style) −

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Table Created.....\n");

   //Reading records
   $stmt = mysqli_prepare($con, "SELECT * FROM Test");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Storing the result
   mysqli_stmt_store_result($stmt);

   //Number of rows
   $count = mysqli_stmt_num_rows($stmt);
   print("Number of rows in the table: ".$count."\n");

   //Closing the statement
   mysqli_stmt_close($stmt);

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

This will produce following result −

Table Created.....
Number of rows in the table: 3

Example

In object oriented style the syntax of this function is $stmt->store_result(); 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();

   //Storing the result
   $stmt->store_result();

   print("Number of rows ".$stmt ->num_rows);

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

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

This will produce following result −

Table Created.....
Number of rows: 3
php_function_reference.htm
Advertisements