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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:16:33+00:00 2026-05-11T19:16:33+00:00

I’ve wrote a quick PHP class to ease the access to a mysql database.

  • 0

I’ve wrote a quick PHP class to ease the access to a mysql database. The class works ok, and has a query() method that opens up the connection, executes the query and then closes the connection (I know that the connection is supposed to close by PHP itself after the script finishes, but I don’t like to rely very much on that).

From a performance standpoint, I know that always opening a connection to a database every time I execute a query may not be a very good practice (and also that when I try to use mysql_real_escape_string() to filter input, it doesn’t work, because there’s no active database connection). But I would like to be more clarified about this. Is it very wrong to do? Why? And I would also like to know about good alternatives to this.

Here’s the class:

class DB {

private $conn;

//database data
private $dbhost;
private $dbname;
private $dbuser;
private $dbpass;

/**
 * Constructor
 * @dbhost string the database host
 * @dbname string the database name
 * @dbuser string the database username
 * @dbpass string the database password
 */
public function __construct ($dbhost, $dbname, $dbuser, $dbpass)
{
    $this->dbhost = $dbhost;
    $this->dbname = $dbname;
    $this->dbuser = $dbuser;
    $this->dbpass = $dbpass;
}

/**
 * Connects to mysql database
 */
private function open ()
{
    $this->conn = mysql_connect ($this->dbhost, $this->dbuser, $this->dbpass)
    or die ("Error connecting to database");

    mysql_select_db ($this->dbname) or die ("Error selecting database");
}

/**
 * Closes the connection to a database
 */
private function close ()
{
    mysql_close($this->conn);
}

/**
 * Executes a given query string
 * @param string $query the query to execute
 * @return mixed the result object on success, False otherwise
 */
public function query ($query)
{   
    $this->open();
    $result = mysql_query($query, $this->conn)
    or die ("Error executing query ".$query." ".mysql_error());

    $this->close();
    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-11T19:16:33+00:00Added an answer on May 11, 2026 at 7:16 pm

    (I know that the connection is supposed to close by PHP itself after the script finishes, but I don’t like to rely very much on that).

    Why? This is a feature of the language. There’s no reason to not trust it. A lot of websites are running just fine counting on PHP to close their stuff. As programmers, of course, we want to close it ourselves. That’s fine. But opening and closing a database connection for every query is a horrible idea. The better way to do it is to call open() from your constructor, and rename your close() to __destruct(). According to the documentation, __destruct will be called "as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence." Sounds like an ideal place to stash away the closing code for your database connection.

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Fixed this by first defining the coverflow container with nothing… May 12, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer Is it possible that Oxygen Editor is handling namespaces differently?… May 12, 2026 at 12:09 am
  • Editorial Team
    Editorial Team added an answer You can have code in the class definition in both… May 12, 2026 at 12:09 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.