PHP mysqli_close() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_close() closes an open MySQLi connection and frees the related client resources.

Object-oriented style uses $mysqli->close(). After close, do not run more queries on that handle. PHP also closes connections at script end, but closing early is clearer in longer scripts.

Syntax

Call mysqli_close() with the values you need. Start with this line.

PHP
mysqli_close($mysqli);
CallWhat it does
mysqli_close($mysqli)Closes the procedural connection handle.
$mysqli->close()Object-oriented close method.
mysqli_close($mysqli) === trueIn modern PHP the function always returns true on a valid close.

From PHP 8.0, mysqli_close() always returns true. Earlier versions could return false on failure. Closing twice on the same object is not useful and should be avoided.

Library declarations
PHP
mysqli_close(mysqli $mysql): true

Parameters

ParameterDescription
mysqlRequired. The mysqli connection to close.

Return Value

Return ValueDescription
trueAlways true in PHP 8+ when the call completes.

PHP Examples

Example 1 — Close After a Simple Query

Open, query, then close. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT 1 AS one");
$row = mysqli_fetch_assoc($result);
echo $row["one"], PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Connect with demo credentials.
  2. Run a tiny SELECT to prove the link works.
  3. Print the value from the result row.
  4. mysqli_close() ends the session cleanly.

Example 2 — Object Style Close

Use the method form after finishing work.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
echo "connected", PHP_EOL;
$mysqli->close();
echo "closed", PHP_EOL;

How It Works

  1. new mysqli() opens the connection.
  2. A status message is printed while still connected.
  3. close() releases the connection.
  4. A second message shows the script continued after close.

Example 3 — Close in a Helper Function

Return early paths can still close the connection in one place.

PHP
<?php
function demoCount(mysqli $db): int {
    $result = mysqli_query($db, "SELECT COUNT(*) AS c FROM products");
    $row = mysqli_fetch_assoc($result);
    return (int) $row["c"];
}

$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
echo demoCount($mysqli), PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. demoCount() runs a COUNT query on the open connection.
  2. The caller prints the returned integer.
  3. mysqli_close() runs once after the helper returns.
  4. Sample output when connected to a demo database shows 42 products.

Frequently Asked Questions

It closes the MySQL connection represented by the mysqli object and frees client-side resources for that handle.
PHP closes open links at the end of the request, but calling close() yourself makes lifetime clear and frees the connection sooner.
Call $mysqli->close(). It matches mysqli_close($mysqli).
No. After close the handle is finished. Open a new connection if you need more queries.
Do not rely on that. Commit or roll back explicitly before closing so the outcome is intentional.

Next: mysqli_commit()

mysqli_commit() permanently saves the current transaction on a MySQLi connection.

mysqli_commit() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful