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 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

For this assignment I had to create my own string class. I initially wrote
Here is a related manager I wrote: class PortfolioItemManager(models.Manager): use_for_related_fields = True def extended(self):
Okay, so i have this code i wrote: class Connection { public static StreamWriter
Here's a Django model class I wrote. This class gets a keyerror when I
Here's a sample Java class that I wrote public class Sleeper { public static
I would like to create a Web Service in Eclipse. I already wrote a
I wrote a class called ButtonTile to extend the SimpleButton class. I then create
I wrote a DB class and I want to create my own model class
I recently wrote a class for an assignment in which I had to store
I wrote the following class for producing monitoring output within an extra window. Unfortunately

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.