MySQLi Tutorial MySQLi References

mysqli_stmt close() Method



The mysqli_stmt::close() / mysqli_stmt_close() function is used to close a prepared statement. It also deallocates the statement handle. If the current statement has pending or unread results, this function cancels them so that the next query can be executed.

Syntax

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

//Procedural style
mysqli_stmt_close(statement)

Parameters

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

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::close() 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
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
$stmt->prepare($query);

//binding parameters
$stmt->bind_param('ssd', $name, $city, $salary);

//set parameters and execute
$name = "John";
$city = "London";
$salary = 2800;
$stmt->execute();

$name = "Marry";
$city = "Paris";
$salary = 2850;
$stmt->execute();

echo "Records inserted successfully.";

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

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

The output of the above code will be similar to:

Records inserted successfully.

Example: Procedural style

The example below shows the usage of mysqli_stmt_close() 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
$stmt = mysqli_stmt_init($mysqli);
$query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
mysqli_stmt_prepare($stmt, $query);

//binding parameters
mysqli_stmt_bind_param($stmt, 'ssd', $name, $city, $salary);

//set parameters and execute
$name = "John";
$city = "London";
$salary = 2800;
mysqli_stmt_execute($stmt);

$name = "Marry";
$city = "Paris";
$salary = 2850;
mysqli_stmt_execute($stmt);

echo "Records inserted successfully.";

//closing the statement
mysqli_stmt_close($stmt);

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

The output of the above code will be similar to:

Records inserted successfully.

❮ MySQLi Functions Reference