English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_stmt_get_result() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_stmt_get_result() function retrieves the result set from the prepared statement.

Definition and Usage

Themysqli_stmt_get_result()The function accepts a statement object as a parameter, retrieves the result set (if any) from the given statement, and returns it.

You cannot use this function to closePersistent Connection.

Syntax

mysqli_stmt_get_result($stmt);

Parameter

Serial NumberParameters and Description
1

con (required)

This is the object representing the prepared statement.

Return Value

If the executed statement is SELECT and successful, the PHP mysqli_stmt_get_result() function will return the result set. In other cases, this function returnsFALSE.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_stmt_get_result()Function Usage (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("Creating table.....\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')");
   print("Inserting record.....\n");
   //Retrieve the content of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM myplayers);
   //Execute statement
   mysqli_stmt_execute($stmt);
   //Get result
   $res = mysqli_stmt_get_result($stmt);
   while ($row = mysqli_fetch_array($res, MYSQLI_NUM)){
      foreach($row as $r){
         print("$r ");
      }
      print("\n");
   }		
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Outputting result

Creating table.....
Inserting record.....
1 Sikhar Dhawan Delhi India
2 Jonathan Trott CapeTown SouthAfrica

Online Example

In the object-oriented style, the syntax of this function is$stmt->get_result();.Here is an example of this function in an object-oriented style;

<?php
   //Establish 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("Creating table.....\n");
   $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   print("Inserting record.....\n");
   //Execute statement
   $stmt->execute();
   //Get result
   $res = $stmt->get_result();
   print_r($res);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Outputting result

Creating table.....
Inserting record.....
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 2
    [lengths] =>
    [num_rows] => 6
    [type] => 0
)

PHP MySQLi Reference Manual