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

  • SEARCH
  • Home
  • 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 6895241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:53:00+00:00 2026-05-27T06:53:00+00:00

For database connection from PHP to MSSQL, I had create the following code. Can

  • 0

For database connection from PHP to MSSQL, I had create the following code. Can somebody suggest me that, is __destruct() function in my code work automatically and close the DB connection or not? Here is my code:

<?php
    class db{
        private $connection;
        private $server, $username, $password, $database, $charset;

        public function __construct($server, $username, $password, $database, $charset){
            $this->server = $server;
            $this->username = $username;
            $this->password = $password;
            $this->database = $database;
            $this->charset = $charset;

            $connectionInfo = array('UID'=>$this->username,
                                    'PWD'=>$this->password,
                                    'Database'=>$this->database,
                                    'CharacterSet'=>$this->charset);

            $this->connection = sqlsrv_connect($this->server, $connectionInfo);

            if ($this->connection === false){
                echo '<h2>Unable to connect to database</h2><br/>';
                die (print_r(sqlsrv_errors(), true));
            };
        }

        public function __destruct(){
            sqlsrv_close($this->connection);
        }

        public function query($query, $params=null){
            $result = sqlsrv_query($this->connection, $query, $params);

            if (!$result){
                echo 'Error in statement execution.\n';
                die(print_r(sqlsrv_errors(), true));
            }

            return $result;
        }
    }
  • 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-05-27T06:53:01+00:00Added an answer on May 27, 2026 at 6:53 am

    Your connection will be closed, except when the __destruct method isn’t called. The following scenarios cause the __destruct not to be executed:

    • If “exit” is called in another destructor
    • Depending on the PHP Version: If “exit” is called in a “register_shutdown_function” function
    • If there is a FATAL error somewhere in the code
    • If there is an exception thrown in another destructor
    • If you try to HANDLE an exception in a destructor ( PHP >= 5.3.0 )

    Check this https://stackoverflow.com/a/2385581/603256

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

Sidebar

Related Questions

I'm working with someone else's database connection PHP function that works fine as long
My problem involves checking if I have a valid database connection before reading from
I have a database connection which takes an input from the querystring to access
In the scenario where a connection is got from a database pool(like Commons DBCP,
I am trying to access database from a Class Library Project. I have connection
I want to access a MySQL database from Java, but remote connection is disabled
Can I store a database connection in the Session object?
I have the following code in php: <?php $con = mysql_connect(localhost,john,john); mysql_select_db(data, $con); if
<?php $connection=mysql_connect('localhost','root',); if(!$connection) { die(database connection failed.mysql_error()); } $db_select=mysql_select_db(quiz_game,$connection); if(!$db_select) { die(database connection failed.mysql_error());
I'm trying a query in PHP to connect and extract data from a MSSQL

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.