Connecting to the DB best practices

Discussion in 'PHP' started by crazyFoo, Feb 23, 2010.

  1. #1
    This is kind of a general question, and I'm sure that there are plenty of ways that people do this, but I wanted to know if what I do is the best way, or if there is room for improvement.

    Typically when I write a database driven site, I create a header.php and a footer.php that I include on each page. I then create a mysql_connect statement in the header, and a mysql_close in the footer.

    Is this the typical way to do it?
    Are there drawbacks to this method?
    Are there some better options?
    Are there any security risks with this method?
     
    crazyFoo, Feb 23, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    I don't want to get too in depth here, but you may want to look at using more MVC style programming. Database and other processor intensive logic occurs outside of files that contain html or user output. This helps to keep code more segmented.
     
    jestep, Feb 23, 2010 IP
  3. darkblade

    darkblade Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think the best way is to create a database class with functions to call your mysql.
    The first result from google gave me a sample database class which you can base your code off it, or just use it. Its up to you.

    http://slaout.linux62.org/php/index.html
     
    darkblade, Feb 23, 2010 IP
  4. tenev

    tenev Active Member

    Messages:
    322
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #4
    use mysql_pconnect instead of connect, would be better for heavy servers :)
     
    tenev, Feb 24, 2010 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    Use the mysqli instead, you then don't have to write your own oo wrapper class.
     
    Kaizoku, Feb 24, 2010 IP
  6. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hello friends, please suggest me easy and best MVC framework.
    I have never used any MVC. This will be my first time So suggest as newbie....
     
    Om ji Kesharwani, Feb 24, 2010 IP
  7. crazyFoo

    crazyFoo Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for all of the suggestions. I do need to learn more about MVC architecture. Do you have any suggestions on how best to learn more how the MVC style of programming works (links, books, etc.)?

    I really like the idea of that class to call all of my SQL. I'll give that a look.
     
    crazyFoo, Feb 24, 2010 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Try the zend framework, it is mvc based.
     
    Kaizoku, Feb 24, 2010 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    I use functions to connect to database, execute a query, and close the connection immediately. I have found this way to work faster than opening a connection at top and closing at bottom. I posted a thread here somewhere with exact times it took to run 30 queries. Opening a connection at query execution worked much faster for some reason.
    You can use a php class, but I find regular functions easier to work with.
    Thanks :)
     
    JEET, Feb 24, 2010 IP
  10. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #10
    JEET, Feb 24, 2010 IP
  11. crazyFoo

    crazyFoo Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    JEET, could you post an example of connecting using functions.

    I'm assuming you do something like this:

    
    <?php
    function getUserInfo($id){
    
      dbConnect();
      $qUserInfo = mysql_query("...Whatever...");
      dbClose();
    
      return $qUserInfo;
    
    }
    
    ?>
    
    PHP:
    Is that what you are talking about?
     
    crazyFoo, Feb 26, 2010 IP
  12. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #12
    Yes, you got me right, something like that, but not writing a seperate function for each query, but a function to execute a mysql query like:

    
    function exec_query($q){
    db_connect(); mysql_query($q); db_close();
    }
    
    $sql= exec_query(" insert into table values() ");
    
    Code (markup):
    Thanks :)
     
    JEET, Feb 26, 2010 IP
  13. Mail Propeller

    Mail Propeller Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I agree. If you have a class, you have a more centralized system that can be used on multiple websites. You can do things like put SQL injection prevention in your functions and other security updates... just a couple ideas.
     
    Mail Propeller, Feb 26, 2010 IP
  14. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Not only this but it also allows users to visit your site if your sql server happens to be lagging for whatever reason. It's much better to show at least something on the site rather than have a blank white screen just sitting there if your sql server is stalling.
     
    AntelopeSalad, Feb 26, 2010 IP
  15. zulugrid

    zulugrid Well-Known Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    108
    #15
    This is generally a bad idea. mysql_pconnect is rarely more efficient than mysql_connect, and mysql_pconnect can cause a pile of problems.
     
    zulugrid, Feb 26, 2010 IP