Results 1 to 3 of 3

Thread: [RESOLVED] Is this database connection handling ok?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Resolved [RESOLVED] Is this database connection handling ok?

    Hello guys,

    I inherited a PHP application. In the db layer of the application, it has this function to connect to a mySql db.

    [phpcode]
    function getConn() {
    return mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
    }
    [/phpcode]

    This function gets called at the beginning of every db function in the dblayer class.

    Beeing new to PHP, I do not know if this is handled well. There's no connection closing on PHP? or it's handled automatically by a connection pool that times out?

    Thanks
    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Is this database connection handling ok?

    Basically, all db connections are closed when the script terminates.
    Also, mysql_connect() and the other DB connections tend to cache handles and return the same handle for the same request.

    However, personally I think you should still keep the connection handle in the class and only call getConn() once.
    (I also think you should use the MDB2 DB abstraction layer if you're using PHP4, and the DBO extension if you're using PHP5, but that's a different story.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Is this database connection handling ok?

    Most code I know makes the connection at the start calls "actions" and closes leaning on the fact that you will mostly only want to talk tot he one database and therefore can get on with called mysql_query.
    ?
    'What's this bit for anyway?
    For Jono

Posting Permissions

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



Click Here to Expand Forum to Full Width