Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO connect and close connection in different functions

Tags:

php

mysql

pdo

Maybe this is a silly question, but I am very new to PDO and am confused. Is it possible to instantiate a PDO object in one function (open a connection to a server/db) and then close the same connection in another function? Will the functions need to be passed the object to be able to close it? I want to do this so I can create a ubiquitous site-wide function that I can call to start a connection, execute non-generic sql, and then close it with another site-wide function. How can I do this? Do I need to pass the object to these function as arguments?

like image 202
eatonphil Avatar asked Dec 06 '22 10:12

eatonphil


2 Answers

Yes, this is possible. I'd recommend using a constructor that initiates a connection to MySQL and a destructor that nullifies it. That way, you won't have to manually call the function every time you want to open and close the connection. The connection will open whenever a new instance of the object is called, and when there are no further references to the object, the connection will be nullified.

It might look something like this:

    private $l; //MySQL Connection

    //Called automatically upon initiation
    function __construct() {
        try {
            $this->l = new PDO("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD); //Initiates connection
            $this->l->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // Sets error mode
        } catch (PDOException $e) {
            file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
            die($e->getMessage()); // Log and display error in the event that there is an issue connecting
        } 
    }

    //Called automatically when there are no further references to object
    function __destruct() {
        try {
            $this->l = null; //Closes connection
        } catch (PDOException $e) {
            file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
            die($e->getMessage());
        }
    }

You might find this reference about constructors and destructors useful: http://php.net/manual/en/language.oop5.decon.php

like image 194
Cody Moncur Avatar answered Dec 28 '22 23:12

Cody Moncur


From my understanding you only have to connect to the database once and don't have to open a connection for each function. The database connection will close automatically but you can close it manually by assigning the null value to the PDO object.

The connection is made when you instantiate the PDO object.

$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);

and closed automatically at the end of the script or when you assign it a null value

$dbh = null;

Helpful guide here

like image 21
grasshopper Avatar answered Dec 29 '22 01:12

grasshopper