Closes an active MySQLi connection.
The examples focus on the operation itself, including the important distinction: mysqli_connect creates a connection.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
mysqli_close(...) — requires the corresponding optional extension
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
Once closed, do not reuse the same handle for queries.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
mysqli_close($link);
echo "Connection closed";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Once closed, do not reuse the same handle for queries. Closing a handle ends its use for further queries.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
mysqli_close($link);
echo "mysqli handle released";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Once closed, do not reuse the same handle for queries. Closing a handle ends its use for further queries.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
mysqli_close($link);
echo "Done with this DB connection";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Result sets and statements should no longer be used after the connection closes.
This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| mysqli_close | Closes an active MySQLi connection. |
| Related API or alternative | mysqli_connect creates a connection. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Result sets and statements should no longer be used after the connection closes. |
| Confusing it with a related API | mysqli_connect creates a connection. |
Key Takeaways
- Closes an active MySQLi connection.
- Result sets and statements should no longer be used after the connection closes.
- mysqli_connect creates a connection.
Use mysqli_close when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.