Use of "global" with mysqli leaves the connection open?

Asked

Viewed 102 times

4

In this form of access to the bank with mysqli I was in doubt whether two connections are opened, and whether, after closing the $stmt, if there is still a connection always open.

The use is basically this:

$host = "mysql.host.com.br";
$db = "dbname";
$user = "user";
$passw = "passw";

$mysqli = new mysqli($host, $dbname, $user, $passw);

GLOBAL $mysqli;

So to use makes:

$stmt = $mysqli->prepare(SELECT...);
$stmt = execute();
...
$stmt->close();

So the first question is whether in this case two connections are opened, one that is used in query with $stmt, and the one that stays open ($mysqli) expecting some other request happen.

Being only one, the connection of the variable $mysqli will remain open until the limit of timeout, or whether when it closes with $stmt it also closes the global ($mysqli)?

  • 1

    Global is in the scope of the script, ceases to exist at the end. PHP doesn’t have session variables, let alone application variables (which I miss a lot, by the way). It would be nice to explain this "leave connection open" better. Any connection is open for a while, while the variable is in scope. The Global only changes the visibility of the variable precisely in relation to the scope, the rest is a consequence.

  • how you’re putting this together, it’s all with include? this will depend on the organization, if you put a page that includes this connection we have only 1 open connection.

  • 1

    That’s right @Virgilionovic, I thought you’d open another one when you do $stmt = $mysqli->prepare, thanks for the complement. abs

  • So @Bacco I was in doubt whether when closing with $stmt->close() It also closed the global $mysqli, but by @Maniero’s response, and thinking about it now, it seems kind of obvious that it doesn’t. It was worth the man strength, since always! abs

  • @What you just said has no relation to the title of the question or the scope that I commented. This close ai is the statement method, not the connection method. The open statement allows you to progress through the records, perform steps, until closed. Then he releases the resources from that statement (which are related to the connection, but are separate things). It would be nice if you reviewed the manual to understand the class that is returned by prepare

  • 1

    Because it is @Bacco, is that you are overating my understanding of the matter rsrs Now that I understand that "The open statement allows you to go forward in the records, exeutar steps, until closed" and that it does not affect the connection that has been opened. I think you’re assuming that I should know this, but I didn’t know.

  • 1

    @Gustavox my assumption based on the comment was of you not knowing, and why I commented what it was :D

  • 1

    The main page of the statement does not help much, but the methods links lead to more details https://www.php.net/manual/en/class.mysqli-stmt.php

  • 1

    Not to be confused with this here, which I really don’t recommend using without a VERY special reason (I’ve never experienced a real situation where it’s beneficial, quite the contrary) https://www.php.net/manual/en/mysqli.persistconns.php

Show 4 more comments

1 answer

3


The question makes no sense, it presupposes that it is opening two connections when clearly the code shows that only one is being opened.

The second code is using the already opened connection, it is not opening a new one. And the closure that occurs there is the query not the connection, the connection is supported by the variable $mysqli, and what is closing is the variable $stmt that supports only the query. Only when do $mysqli->close() is that the connection will be closed (or if the script shutdown, which if you go to see the whole context of the code looks like that’s when this connection will be closed).

  • Meaning makes, so much so that you answered what I wanted to know. See "So the first question is whether in the case are open two connections". Anyway thanks for the ultra-fast mega response! : ) abs

Browser other questions tagged

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