Professional Web Applications Themes

Sessions and persistent connections to MySQL database - PHP Development

I'm relatively new to PHP/MySQL and I've heard about maintaining a persistent connection to the database. To save overhead, I'd like to maintain the connection in a session variable, but I have a few questions first. I'm able to establish session variables and test them with isset() to avoid re-defining them with each new web-page load. So is this really as simple as if (!isset($_SESSION['connection'])) { $connection = mysql_connect("SomeServer","UserName","password"); $_SESSION['connection'] = $connection; }; Will this work? and is there a better way to do it--or a more conventional way? What should I be thinking about vis a vis security regarding ...

  1. #1

    Default Sessions and persistent connections to MySQL database

    I'm relatively new to PHP/MySQL and I've heard about maintaining a
    persistent connection to the database. To save overhead, I'd like to
    maintain the connection in a session variable, but I have a few
    questions first.

    I'm able to establish session variables and test them with isset() to
    avoid re-defining them with each new web-page load. So is this really
    as simple as

    if (!isset($_SESSION['connection'])) {
    $connection = mysql_connect("SomeServer","UserName","password");
    $_SESSION['connection'] = $connection;
    };

    Will this work? and is there a better way to do it--or a more
    conventional way? What should I be thinking about vis a vis security
    regarding this connection.

    Is there a recommended way to make the session's connection variable
    "expire"? I suppose I could store the time the connection was
    established and then check the elapsed time with each web-page
    re-load, dumping the session if it's been too long.

    Is there a preferred way to store $connection without using
    $_SESSION[]?

    Thanks,
    M. Katz
    M. Guest

  2. #2

    Default Re: Sessions and persistent connections to MySQL database

    M. Katz wrote:
     

    I'm not sure that this will work. A so created MySQL connection will be
    closed when the script ends. You will store the handle to connection in
    the session variable but it will invalid since the real connection will
    be closed between to script calls.

    There is another function in the MySQL module for opening persistent
    connections: mysql_pconnect.
     

    Using mysql_pconnect, the connection life-time will be managed by PHP.
    You don't need to make any explicite connection closure and after a
    while, remaining connections will be automatically closed by PHP.
     

    Guillaume Brocker
    Guillaume Guest

  3. #3

    Default Re: Sessions and persistent connections to MySQL database

    M. Katz wrote: 

    If I am not entirely correct, somebody correct me....
    MySQL connection is a resource variable and thus cannot
    be used and stored into a session. It will not work.
    I tried it, before I found out this, and it did not. Maybe I
    did something wrong....

    For what you want to accomplish use mysql_pconnect which gives
    you a reusable persistent connection to mysql.

    Tomy.


    Tomislav Guest

  4. #4

    Default Re: Sessions and persistent connections to MySQL database

    On 10 Feb 2004 00:39:53 -0800, M. Katz wrote:
     
    you must use mysql_pconnect instead
     
    i think that you dont have to store it in session variables
    just use permanent connection (mysql_pconnect).
    It's all explained in PHP manual

    Krečo Guest

  5. #5

    Default Re: Sessions and persistent connections to MySQL database

    Hi M!

    On 10 Feb 2004 00:39:53 -0800, com (M. Katz) wrote:
     

    Not dependant on if it works, I believe the overhead to connect to
    MySQL is lower than keeping the session data on the webserver.

    HTH, Jochen
    --
    Jochen Daum - Cabletalk Group Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    http://sourceforge.net/projects/phpdbedittk/
    Jochen Guest

Similar Threads

  1. Connection Pooling and Persistent Connections
    By Steve in forum PHP Development
    Replies: 2
    Last Post: February 1st, 04:41 AM
  2. MSSQL persistent connections and PHP
    By Chung Leong in forum PHP Development
    Replies: 0
    Last Post: December 8th, 01:05 AM
  3. mysql_pconnect() Do persistent connections ever die?
    By Randell D. in forum PHP Development
    Replies: 3
    Last Post: October 10th, 11:04 PM
  4. [PHP] Persistent database connections in PHP
    By Cesar Aracena in forum PHP Development
    Replies: 4
    Last Post: September 5th, 10:04 AM
  5. Persistent database connections in PHP
    By David Brannlund in forum PHP Development
    Replies: 0
    Last Post: September 5th, 06:05 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139