I believe that perhaps in the documentation it has not been made clear, it is not a question of closing at the end with mysqli_close
or automatically, what the documentation that is to say it is recommended to close when is not more using, ie if you will no longer use close.
Imagine you have a script that takes time to process, but you only need the bank results at the beginning, it would hold the connection to the bank until the script is finished, even if you put mysqli_close
at the end of the script. In a lengthy script, if it closes right after getting the bank result and the rest of the long process follow, it will probably help improve the performance of the bank server.
Now if you are going to use more than one location in your script there’s no point in closing and reopening, it would be better to keep open, because until the time to close and reopen can be something relatively costly (not always), have to analyze calmly how developed your script and see the need, in most cases will be used at the beginning and at the end "leave open" and the script automatically killing the connection (which is natural for them) already solves, but there will be isolated cases where probably only need to use in a part of the script, I would go even further, in fact it is an error that I see that most (most of them) commits, people connect with banks (mysql, sqlserver, etc.) without needing, for example, create a header.php
and put something like:
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
?>
<nav>
...
</nav>
And on every page something like:
<html>
...
<body>
...
<?php require 'header.php'; ?>
...
In short, on any page there is always a connection to the bank and many of them may be unnecessary, because not every page will access something, or will need at that time.
The ideal would be to connect only if it is necessary to order something, a rather simple example would be to use with caution, making a function to connect quite simple:
function my_query($query)
{
static $link;
if (!$link) {
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
}
return mysqli_query($link, $query);
}
This is just an illustrative example, the idea is only to explain that the connection will only be made if a query is needed, which would run something like:
$result = my_query($query);
Because of static
the function will connect and keep in the variable there the result, of course there is a not very good example of implementation, it is only illustrative even, there are several ways to do, but the question is you understand that the problem is not only close at the beginning or as soon as possible, is to close if you are no longer using, so that it does not become an extra client on your database server.
No unnecessary connections for sure your bank server will get less work and will probably respond better.
That question already has that answer and that other answer.
– LipESprY
Rod and @Lipespry believe that both links are not incorrect, but do not address the real problem about opening and closing or persisting connections, I believe that by far someone has understood that the problem is in fact the strategy of when to keep an open connection and when to close prematurely, I tried to answer in https://answall.com/a/376418/3635, If you have any doubts or disagreement the questions for improvement or even criticism are welcome.
– Guilherme Nascimento