mysqli::close

mysqli_close

(PHP 5, PHP 7, PHP 8)

mysqli::close -- mysqli_close — Closes a previously opened database connection

Description

Object-oriented style

public function mysqli::close(): true

Procedural style

function mysqli_close(mysqli $mysql): true

Closes a previously opened database connection.

Open non-persistent MySQL connections and result sets are automatically closed when their objects are destroyed. Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

Parameters

mysql

Procedural style only: A mysqli object returned by mysqli_connect() or mysqli_init()

Return Values

Always returns true.

Changelog

Version Description
8.0.0 This function now always returns true. Previously it returned false on failure.

Examples

Example #1 mysqli::close() example

Object-oriented style

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$result = $mysqli->query("SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3");

/* Close the connection as soon as it's no longer needed */
$mysqli->close();

foreach ($result as $row) {
    /* Processing of the data retrieved from the database */
}

Procedural style

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");

$result = mysqli_query($mysqli, "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3");

/* Close the connection as soon as it's no longer needed */
mysqli_close($mysqli);

foreach ($result as $row) {
    /* Processing of the data retrieved from the database */
}

Notes

Note:

mysqli_close() will not close persistent connections. For additional details, see the manual page on persistent connections.

See Also

+add a note

User Contributed Notes 2 notes

up
0
user123 at gmail dot com
6 days ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php 
        include("passwd.php");
        $mysqli = mysqli_connect( $host, $user, $password, $dbname);

        if(isset($_GET['usun'])) {
            $id = $_GET['usun'];
            mysqli_query($mysqli, "DELETE FROM dane WHERE id=$id" );
            header("Location: index3.php");
            exit;

        }

        if (!$mysqli) {
            echo "Error: Unable to connect to MySQL." . PHP_EOL;
            echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
            echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }

        if(isset($_POST['dodaj'])) {

            $kraj = $_POST['kraje'];
            $id = $_POST['id'];
            $nominal = $_POST['nominal'];
            $materialy = $_POST['materialy'];
            $sql = "INSERT INTO dane (kraj_id, nr_kat, nominal, stopa_id)
            VALUES ($kraj, $id, $nominal, $materialy)";

            mysqli_query($mysqli, $sql);
        }

        $result = mysqli_query($mysqli, "SELECT dane.id as id_dane, dane.*, flagi.*, materialy.* FROM dane 
        JOIN flagi on dane.kraj_id = flagi.id
        JOIN materialy on dane.stopa_id = materialy.id
        ");
        $all = mysqli_fetch_all($result, MYSQLI_ASSOC);

        echo "<table border='1' style='border-collapse: collapse'>";
        echo "<tr>
                <th>Flaga</th>
                <th>Nazwa kraju</th>
                <th>id</th>
                <th>nominał</th>
                <th>material</th>
                <th>usun</th>
              </tr>
        ";

        foreach ($all as $a ) {
            echo "<tr>";
            echo "<td><img src='../gfx/" . $a['plik'] . "' style='width: 50px;'></td>";
            echo "<td>" . $a["kraj"] . "</td>";
            echo "<td>" . $a["id_dane"] . "</td>";
            echo "<td>" . $a["nominal"] . "</td>";
            echo "<td>" . $a["nazwa"] . "</td>";
            echo "<td><a href='index3.php?usun=" . $a["id_dane"] . "'> usun </a></td>";
            echo "</tr>";
        }
            echo "<tr><td colspan='6'>DODAWANIE PONIŻEJ</td></tr>";

     
                  
$kraje = mysqli_fetch_all(mysqli_query($mysqli, "SELECT * FROM flagi"), MYSQLI_ASSOC);
$nominal = mysqli_fetch_all(mysqli_query($mysqli, "SELECT * FROM materialy"), MYSQLI_ASSOC);

echo "<form method='post'>
<tr>
    <td>Automatyczne</td>
    <td>
        <select name='kraje'>";

        foreach ($kraje as $k) {
            echo '<option value="' . $k["id"] . '">' . $k["kraj"] . '</option>';
        }

echo "</select>
    </td>
    <td>
        <input type='number' placeholder='id' name='id'>
    </td>
    <td>
        <input type='text' placeholder='nominal' name='nominal'>
    </td>
    <td>
        <select name='materialy'>";

        foreach ($nominal as $n) {
            echo '<option value="' . $n["id"] . '">' . $n["nazwa"] . '</option>';
        }

echo "</select>
    </td>
    <td>
        <button type='submit' name='dodaj'>OK</button>
    </td>
</tr>
</form>";

        echo "</table>";

    ?>
</body>
</html>
up
0
nunoandredev at gmail dot com
10 months ago
Be aware that when using multiple files that are dependent from each other, if by mistake mysqli::close is called twice, the second call will throw a Fatal Error HTTP 500 even though the page renders normally.
To Top