Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:29:24+00:00 2026-06-14T23:29:24+00:00

Maybe this is a silly question, but I am very new to PDO and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:29:25+00:00Added an answer on June 14, 2026 at 11:29 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a very silly question but I'm new using django. I have
It this possible? How? (Maybe silly question, but I couldn't find answer :( )
Duplicate : Function overloading by return type? Maybe this is a very silly question
Maybe this is a very silly question, but the book I'm reading instructed me
Maybe it is a silly question but I am a very confused with JavaScript.
This maybe a silly question, but I need confirmation and I have no one
Ok so this maybe a simple/silly question but I don't know so here goes:
Maybe this is a silly question, but I'm working on a project that wants
This may be a silly question, but as someone relatively new to PHP, I'm
My question may be very silly but here it is. I went through this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.