MySQLi Tutorial MySQLi References

mysqli_stmt get_result() Method



The mysqli_stmt::get_result() / mysqli_stmt_get_result() function retrieves a result set from a prepared statement as a mysqli_result object. The data will be fetched from the MySQL server to PHP. This method should be called only for queries which produce a result set.

Note: This function cannot be used together with mysqli_stmt_store_result(). Both of these functions retrieve the full result set from the MySQL server.

Note: Available only with mysqlnd.

Syntax

//Object-oriented style
public mysqli_stmt::get_result()

//Procedural style
mysqli_stmt_get_result(statement)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().

Return Value

Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, this function will return a mysqli_result object. For other successful queries, this function will return false.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::get_result() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//creating a prepared statement
$sql = "SELECT Name, Age, City FROM Employee WHERE City=?";
$stmt = $mysqli->prepare($sql);

//binding parameters for markers
$stmt->bind_param("s", $city);

$cityList = array('London', 'Paris', 'Mumbai');

//executing the SQL statement for each $city
foreach ($cityList as $city) {
  $stmt->execute();
  $result = $stmt->get_result();
  while ($row = $result->fetch_array(MYSQLI_NUM)) {
    foreach ($row as $r) {
      print "$r ";
    }
    print "\n";
  }
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

John 28 London
Marry 25 London
Kim 30 Paris 
Akash 27 Mumbai 
Ramesh 25 Mumbai 
Sachin 31 Mumbai 

Example: Procedural style

The example below shows the usage of mysqli_stmt_get_result() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//creating a prepared statement
$sql = "SELECT Name, Age, City FROM Employee WHERE City=?";
$stmt = mysqli_prepare($mysqli, $sql);

//binding parameters for markers
mysqli_stmt_bind_param($stmt, "s", $city);

$cityList = array('London', 'Paris', 'Mumbai');

//executing the SQL statement for each $city
foreach ($cityList as $city) {
  mysqli_stmt_execute($stmt);
  $result = mysqli_stmt_get_result($stmt);
  while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    foreach ($row as $r) {
      print "$r ";
    }
    print "\n";
  }
}

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

John 28 London
Marry 25 London
Kim 30 Paris 
Akash 27 Mumbai 
Ramesh 25 Mumbai 
Sachin 31 Mumbai 

❮ MySQLi Functions Reference