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 843627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:09:31+00:00 2026-05-15T06:09:31+00:00

I wrote a class to create an automated connection with MySQL and create queries.

  • 0

I wrote a class to create an automated connection with MySQL and create queries. Here’s how it looks like:

include("constants.php");

class MySQLDB {
    var $connection;

    function __construct() {
        $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
        mysql_select_db(DB_NAME, $this->connection);
        mysql_set_charset('utf8', $this->connection);
    }

    // SELECT ALL FROM
    function sf($unit, $table) {
        return mysql_query("SELECT ".$unit." FROM ".$table, $this->connection);
    }
        // and so on...
}

$mysql = new MySQLDB;

Now, I thought it would be better if I close the connection after I run some of this functions in other php pages. So how do I do that (the most effective way) in this class?

I tried adding mysql_close($this->connection); at the end of the class (before the close bracket) but it gives me an error.

  • 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-15T06:09:32+00:00Added an answer on May 15, 2026 at 6:09 am

    You’d need to place that code in a function named __destruct(), much in the same way as __construct(). See http://php.net/manual/en/language.oop5.decon.php for more information.

    The code would then look like:

    include("constants.php");
    
    class MySQLDB {
        var $connection;
        var $sf;
    
        function __construct() {
            $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
            mysql_select_db(DB_NAME, $this->connection);
            mysql_set_charset('utf8', $this->connection);
        }
    
        // SELECT ALL FROM
        function sf($unit, $table) {
            return mysql_query("SELECT ".$unit." FROM ".$table, $this->connection);
            $this->sf();
        }
            // and so on...
    
        function __destruct() {
            mysql_close($this->connection);
        }
    }
    

    Please note that you don’t know exactly when this method is run: that depends on when the object is garbage collected. But as Ken noted below, executing mysql_close() is good for symmetry, but not necessary to free resources.

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

Sidebar

Related Questions

I recently wrote a class for an assignment in which I had to store
I'm getting some really wierd linking errors from a class I wrote. I am
I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
We wrote a small Windows class library that implements extension methods for some standard
I once wrote this line in a Java class. This compiled fine in Eclipse
I recently wrote a small tool to generate a class for each tier I
I recently wrote a DLL in C# (.Net 2.0) which contains a class that
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote
I wrote a CLR class with several methods, which are linked as functions in
For my project I wrote a small config class that loads its data from

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.