• PHP Video Tutorials

PHP mysqli_stmt_free_result() Function



Definition and Usage

The mysqli_stmt_free_result() function accepts a (prepared) statement object as a parameter, and frees the memory in which the result of the given statement is stored (when you store the result using the mysqli_stmt_store_result() function).

Syntax

mysqli_stmt_free_result($stmt);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a prepared statement.

Return Values

The PHP mysqli_stmt_free_result() function doesn't 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_free_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");

   //Freeing the resultset
   mysqli_stmt_free_result($stmt);

   $count = mysqli_stmt_num_rows($stmt);
   print("Number of rows after freeing the result: ".$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
Number of rows after freeing the result: 0

Example

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

   //Freeing the resultset memory
   $stmt->free_result();

   //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