3 Replies - 766 Views - Last Post: 15 March 2012 - 12:14 AM Rate Topic: -----

#1 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Connecting to MySQL without opening a new connection every time.

Posted 14 March 2012 - 08:40 PM

Hi so my web application in php needs to retrieve,store and update data at almost every page there is. Right now I have it so every page connects to the database and then uses mysql_close(). So every time it has to connect to the database. It's common sense that this is not efficient. How would i accomplish the connection without having to reconnect every time. Do I use a Session variable for connect? And is it safe to be connected to the database the whole time the user is on the app? Thank you and please tell me if I did not make something clear. Thanks

This post has been edited by Atli: 14 March 2012 - 09:21 PM
Reason for edit:: Thread title updated for clarity. Please use meaningful, descriptive titles.


Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to MySQL without opening a new connection every time.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Connecting to MySQL without opening a new connection every time.

Posted 14 March 2012 - 09:18 PM

Those are called persistent connections. The way in which you create a persistent connection depends on which extension you are using to connect to MySQL.

  • For PDO, set PDO::ATTR_PERSISTENT => true in the driver options when creating the connection.
    $dsn = "mysql:host=localhost;dbname=test";
    $dbLink = new PDO($dsn, $user, $pwd, array(
        PDO::ATTR_PERSISTENT => true
    ));
    
    


  • For MySQLi, simply add p: to the host name of the connection. (Requires PHP 5.3 or higher).
    $dbLink = new mysqli("p:localhost", "user", "pwd", "schema");
    


  • For the old-school mysql_connect function, use the mysql_pconnect instead.

Was This Post Helpful? 2
  • +
  • -

#3 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Re: Connecting to MySQL without opening a new connection every time.

Posted 14 March 2012 - 11:59 PM

Does it matter which method I use? Or is it just preference. And for the first two methods will i just have to put that at the top of my first page and the other pages will be able to run queries? And last question how would I be able to close the connection after the user quits the page because mysql_close() won't work on mysql_pconnect and I'm assuming the other ones too.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Connecting to MySQL without opening a new connection every time.

Posted 15 March 2012 - 12:14 AM

why do you want to close a persistent connection? (note: the Manual has a good read for when to (not) use persistent connections)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1