Close PHP and ASP connection or not?

Asked

Viewed 248 times

0

Making my applications in ASP I make a include at the end of all pages by disconnecting the bank.

In PHP you need to disconnect too? another question: How to close this PHP connection?

$conexao = mysqli_connect('127.0.0.1','root','');
$banco = mysqli_select_db($conexao,'meu-banco');
  • That question already has that answer and that other answer.

  • 1

    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.

2 answers

3


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.

  • +1 for the part where it says: "- an error that I see that most (most) commit". I have made this mistake based on numerous articles that make this same mistake. We end up "following" without thinking. It was super interesting to comment on your reply! In fact, I think it would be useful to merge the answers to this and the other questions I mentioned in my comment upstairs...

  • Thanks dear @Lipespry! About the merge, I was a little confused, could I explain your suggestion better? Thanks again

  • Merging would be to turn the three questions into a single one and put together all the answers (or else mark two as duplicates of the third one) since they all deal with the same subject: close or not connection to the database (and all are about PHP and Mysql, in addition to ASP as a bonus of this one). But this is a service for moderation. The most I can (day) do was comment on the links of the other two in order to relate them. Note that your answer answers the three questions, but if one reader falls for the other two, he will lose the content of this.

  • 1

    @Lipespry a yes, it makes sense to even mark as dup (that already exists in the tools of the site), I can do this without approval of other votes, but I prefer to expect the intention of the other participants of the community. But your idea on this will certainly be the best, I’ll talk to the staff tomorrow and see if they agree. Thank you ;)

  • I conclude that marking as a duplicate may be a better option since the search engines have already indexed the other pages. Unless moderation creates a redirect at the addresses of the three pages. There it is with them. ;D

  • 2

    I thought of the mixture of one and the other, but I would need to read everything very calmly. I’ll probably only see this later in the day. cc @Lipespry

Show 1 more comment

0

To close a connection, use the function mysqli_close:

mysqli_close($conexao);

Straight from the documentation:

Non-persistent Mysql connections and result sets are automatically destroyed when a PHP script finishes its execution. Therefore, while explicitly closing open connections and releasing result sets is optional, this is recommended. This will immediately return resources to PHP and Mysql, which can improve performance. For related information, see releasing resources

Source: https://www.php.net/manual/en/mysqli.close.php

  • Show, it is not necessary but it is recommended. So I will close as I do in ASP. Thanks.

  • 3

    This is half true. You have to understand the whole context, because if you need the resource right away it can be more costly to open a new connection. In addition, Mysql variables, transactions, last id are stored per session/connection, so closing and reopening a connection can be a bug depending on the case (just as leaving it open is wrong in other contexts because it is a limited resource).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.