SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Connection Instance

    Hi,

    I want to make one database connection per user and use the same connection object in all other php pages. How can I do this?

    I have a class called dbconn with function as connect(), select(), update(), delete(). The way I know to establish a connection from another php file is to include dbconn class, create object and then call function. But the problem is I will have to repeat this in almost every php file located in different folders.

    So, isn't there any way to initiate the object once and then use it accross php pages?

    Thanks!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,839
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jluniya View Post

    So, isn't there any way to initiate the object once and then use it accross php pages?

    Thanks!
    Yes - you put the code in one file and then include that file in all the web pages. Using require_once to do the include is probably best since the page will not work without it and you don't want it accidentally being included more than once which also will not work.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    require_once or include_once will work fine. So you mean in every php page I will have to call this and initiate object. But I was looking for the option where I can initiate DB connection object once and can use it accross the pages untill user's session is alive. Is there any way to do this?

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jluniya View Post
    require_once or include_once will work fine. So you mean in every php page I will have to call this and initiate object. But I was looking for the option where I can initiate DB connection object once and can use it across the pages untill user's session is alive. Is there any way to do this?
    No, there isn't. The way PHP works is that all data associated with a running script are discarded when the script has finished executing and any new instance of this script begins with a clean state. You can use sessions to store data across multiple page requests but sessions cannot store objects which are pointers to database connections, you have to initiate the object every time.

    You can use persistent connections to reuse the connection across page requests but still you will need to create a new db object on every request. Persistent connections can lead you to unexpected problems if you don't know what you are doing so it's safest not to use them:

    http://php.net/manual/en/features.pe...onnections.php
    http://php.net/manual/en/mysqli.persistconns.php
    http://php.net/manual/en/pdo.connections.php

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! It helps!


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
  •