www.webdeveloper.com
Results 1 to 5 of 5

Thread: mysql_pconnect or mysql_connect ?

  1. #1
    Join Date
    Jun 2006
    Posts
    46

    mysql_pconnect or mysql_connect ?

    I hope someone can answer these questions.

    I've done some research and decided to use mysql_pconnect for the following reasons.

    I've written my own CMS using objects in PHP. All the objects are in one file along with the connection function. (Please advise if seperate files are recommended). The objects file are included in most pages. Most functions within objects connect to the database but do not close the connection. Normally, I'd think this is a bad thing but if mysql_pconnect is being used, do I have to close the connect in each function?

    Please correct me if I'm wrong; the mysql_pconnect simply looks for an existing connection and uses the same one virtually using only one connection.

    Any help would be much apprecaited.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,145
    You might want to read this PHP manual page: http://www.php.net/manual/en/feature...onnections.php

    Whether or not you opt to use persistent connections, I would not recommend having each class that needs DB access doing its own connection. Instead, I would have one database connection class, instantiate it to create the connection, then pass that object to each other object's constructor that needs it. If you use MySQLi instead of the older MySQL extension, you can just instantiate a MySQLi object and pass that to each applicable object.

    PHP Code:
    <?php
    class Foo
    {
       private 
    $db;
       public function 
    __construct(MySQLi $db)
       {
          
    $this->db $db;
       }
       public function 
    bar($id)
       {
          
    $sql "SELECT * FROM table WHERE id=" . (int) $id;
          
    $result $this->db->query($sql);
          return 
    $result->fetch_all(MYSQLI_ASSOC);
       }
    }

    // USAGE:
    $db = new mysqli($dbHost$dbUser$dbPass$dbName);
    $foo = new Foo($db);
    $data $foo->bar(1);
    Last edited by NogDog; 10-01-2010 at 01:50 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2006
    Posts
    46
    Thanks for responding NogDog.

    That is what I've done. Maybe an example would help explain.

    I created a connection class that accepts values in order to make the connection.

    Connection Class:
    Code:
    class db_connection{
                // database connection
                 function db_con($host,$name,$password){
                     mysql_pconnect("$host","$name","$password") or die(mysql_error());
                     mysql_select_db($database);
                }
            
        }
    Then I've created other classes with function to actually do the work.

    Example:
    Code:
    //load selected blog title into editor    
            function editor_show_post_tle(){
                        $pg=$_GET["pg"];
                        $qry_edit_title="SELECT * FROM blog WHERE blog_title='$pg'";
                        $title_result=mysql_query($qry_edit_title);                
                        while($row_page_title=mysql_fetch_array($title_result)){
                            echo $row_page_title["blog_title"];
                        }
              // should there be a close connection here?
            }
    As you can see the connection has been made ( providing the values are sent to the variables) and the function doesn't have a mysql_close() within it.

    Imagine many functions in the same manner with not mysql_close(). Would this cause an issue and should I be closing the database within each function?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,145
    No, you do not want to close the connection as part of the query function, as any subsequent queries would then fail unless you then did another mysql_[p]connect() first. The persistent aspect of the pconnect only affects subsequent calls to mysql_pconnect(), not calls to any other mysql_*() function. In fact, if you do close it, then there is no reason to use mysql_pconnect(), as a closed connection is a closed connection, regardless of whether it was persistent.

    PS: I've generally avoided persistent connections, as they can become problematic if anything is not configured just right, with orphaned connections using up your allowed limit of connections. They can make a performance difference in some cases, and you can always implement them later if you determine you need to find some performance boosts and are willing to keep an eye on things and tweak web server and database server settings to address any problems that arise. Generally, application design and database/query optimization will do much more to improve performance than using mysql_pconnect() once per script instead of mysql_connect() once per script.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2006
    Posts
    46

    Thanks for all your help

    Thanks NogDog for all your help. I appreciate your time.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles