I have created a class which connects to the DB. All other classes can then use the connect function within this class to open a connection to the DB. At the end of the function, I return the results. So how do I close the connection after I have returned the results?
<?php
class DbhPdo {
private $servername;
private $username;
private $pwd;
private $dbname;
protected function connect() {
$this->servername = "localhost";
$this->username = "someUser";
$this->pwd = "somePswd";
$this->dbname = "someDB";
$this->charset = "utf8mb4";
try{
$dsn = "mysql:host=" . $this->servername . ";dbname=" . $this->dbname . ";charset=" . $this->charset;
$pdo = new PDO($dsn, $this->username, $this->pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch(PDOException $e) {
echo "Message: " . $e->getMessage();
}
}
}
If I set the following after the return, then it is never called to close the connection:
$pdo = null;
Or does PDO close this connection automatically because it is done executing commands after - return $pdo?
Or do I have to close the connection in the class that extended the connection?
The following is the class that extends the aforementioned class, but I am returning the results as well in this class, so I can not set stmt to null here either:
<?php
require_once('dbh.pdo.inc.php');
class LoginPdo extends DbhPdo {
public $name;
public $pass1;
public $hashed;
public $salted;
protected function getSomething($name) {
try{
$stmt = $this->connect()->query("select something from table where name ='" . $name . "'");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}catch (PDOException $e){
echo 'Message: ' . $e->getMessage();
}
}
}
Which leaves the original class that started this:
try{
$this->result = $this->getSomething($this->name);
echo json_encode($this->result);
$this->result = null;
}catch (PDOException $e){
echo 'Message: ' . $e->getMessage();
}
Is setting the $this->result = null going to close the connection? If it does, then I do not understand. Can someone explain it to me? Or do I understand it correctly when setting $pdo = null would be where I need to close the connection?
If so, then how can I set it to null after the return $pdo is executed?
Thanks in advance
Update: Just in case someone else wants to be able to verify if a connection was closed, these were the steps that I took to confirm @Don'tPanic comments to tail the general log files:
mysql -u root -p
show variables like '%log%';
set global general_log=ON;
tail -f /usr/local/mysql/data/<your log file's name>.log
Which then showed the connection, as I used PostMan to post the following query were opened and closed instantly:
190130 11:00:17 2581 Query show variables like '%log%'
190130 11:02:14 2582 Connect root@localhost on <table name>
2582 Query select * from something where name ='whatever'
2582 Quit
This was accomplished by following @Don'tPanic answer to add a new function in the DbhPdo class:
protected function disconnect() {
$this->pdo = null;
}
Then I added $this->pdo = null; after echo json_encode($this->result);
Thank you for all the comments