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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:36:00+00:00 2026-05-13T13:36:00+00:00

I’m pretty sick of having to rewrite my code every time I learn something

  • 0

I’m pretty sick of having to rewrite my code every time I learn something new about php (like the fact that mysql connections cannot be passed around in a session as a handle).

How do you implement mysql connection in your projects? A lot of people have proposed “connection pooling”, but after reading the manual i’m still lost. It’s like: “connection pooling is mysql_pconnect!” – me: “and…? how is that any different in reality? can you pass around a mysql_pconnect in a session? why is this seemingly mysterious aura??”

Let me explain my situation. I have a function called “query1”:

function query1($query)
{
    $db = new mysql(HOST,USER,PASS,DBNAME);
    $result = $db->query($query);
    $db->close();
    return $result;
} 

This is seems like a squanderous and inefficient way of querying a db (especially since you need a mysql handle for functions like mysql_real_escape_string). What is the correct form to do it? Can someone please help me?

Thank you I’d really appreciate a good honest answer.

  • 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-13T13:36:00+00:00Added an answer on May 13, 2026 at 1:36 pm

    Normally connections happen once a page load. AKA

    class Database{
        public function connect()
        {
             $this->connection = mysql_connect();
        }
    
        // This will be called at the end of the script.
        public function __destruct()
        {
            mysql_close($this->connection);
        }
    
        public function query($query)
        {
            return mysql_query($query, $this->connection);
        }
    }
    $database = new Database;
    $database->connect();
    
    $database->query("INSERT INTO TABLE (`Name`) VALUES('Chacha')");
    

    Basically, you open the connection in the beginning of the page, close it at the end page. Then, you can make various queries during the page and don’t have to do anything to the connection.

    You could even do the mysql_connect in the constructor as Erik suggest.


    To use the above using global variables (not suggested as it creates global state), you would do something like

    Global $db;
    
    $db = new Database;
    // ... do startup stuff
    
    function doSomething()
    {
        Global $db;
        $db->query("Do Something");
    }
    

    Oh, and no one mentioned you don’t have to pass around a parameter. Just connect

    mysql_connect();
    

    Then, mysql_query will just use the last connection no matter what the scope is.

    mysql_connect();
    
    function doSomething()
    {
        mysql_query("Do something");
    }
    

    Per the comments:

    I think you should use mysql_pconnect() instead of mysql_connect(), because mysql_connect() doesn’t use connection pooling. – nightcoder

    You might want to consider whether you use mysql_connect or mysql_pconnect. However, you should still only connect once per script.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.