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
)?
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.
– Bacco
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.– novic
That’s right @Virgilionovic, I thought you’d open another one when you do
$stmt = $mysqli->prepare
, thanks for the complement. abs– gustavox
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– gustavox
@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
– Bacco
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.
– gustavox
@Gustavox my assumption based on the comment was of you not knowing, and why I commented what it was :D
– Bacco
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
– Bacco
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
– Bacco