SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: how often do i need to connect mysql db in my php website??

  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how often do i need to connect mysql db in my php website??

    Hi everybody

    Could you please tell me,

    how often do i need to connect mysql db in my php website??

    i have created new mysql object but i worried about using it in top of pages or

    in when i have to query something ??

    help me please?

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Only once each time the page is loaded.

    Every time a page is loaded (assuming you aren't using a persistent connection), it's like you are starting out fresh with your PHP, so everything is processed again, including the connection. Once you're connected, you're connected until that page is finished (or something extremely unusual happens).

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ideally, you would only create a connection when you need it. You could, create a function to only create a connection if/when you need it , something similar to:-
    PHP Code:

    <?php
    function getConnection(){
      static 
    $connection;
      if(
    false === is_resource($connection)){
        
    $connection mysql_connect('localhost''user''pass');
        if(
    false === is_resource($connection) || false === mysql_select_db('database')){
          
    trigger_error(mysql_error(), E_USER_WARNING);
          return 
    false;
        }
      }
      return 
    $connection;
    }
    You can then include this function whenever you like, but it will only create a connection when called.

    PHP Code:
    mysql_query('SELECT bacon FROM breakfast'getConnection()); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Member
    Join Date
    May 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,,thank you
    but which is the best way for large website and large a mount of queries ?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    More often that not, you'll only ever really have one connection; regardless of the number of queries.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    What Anthony was getting at is don't create a connection if you aren't going to use it on that particular page.

    That function Anthony provided can be used anytime you need to run a query somewhere.

    You would use it something like:
    Code:
    $conn = getConnection();
    mysql_query("SELECT * FROM bob", $conn);
    Since getConnection() uses a static variable, it'll only ever create one connection (even if you call it multiple times).

    The simpler, slightly less efficient but more traditional method is to just create a connection at the top of your script and use it throughout the rest of your page.

    There are only a very few rare cases I can think of you might want more than one connection. 99.9% of the cases you'll just need one connection.

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    1,917
    Mentioned
    33 Post(s)
    Tagged
    0 Thread(s)
    One of those rare cases is an importer script moving data between databases while converting its structure.

    Also, PDO is a much better way to manage the connection than the old mysql library of functions.

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
  •