mysql_connect

Discussion in 'PHP' started by cro91, Oct 20, 2011.

  1. #1
    I was going to include a few small php scripts within my html page and was wanting to know if I need to include a mysql_connect for each one of can I just do one in the header?
     
    cro91, Oct 20, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    You only need to connect to your mysql database once ;-)
     
    ssmm987, Oct 20, 2011 IP
  3. P1raten

    P1raten Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    The mysql function is unsafe and outdated.

    Use mysqli instead.
     
    P1raten, Oct 20, 2011 IP
  4. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I didn't know that. Why was it deemed unsafe? Whats the benefits of using mysqli?
     
    cro91, Oct 20, 2011 IP
  5. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #5
    It's not unsafe. It's an alternative. mysql_connect() is often use in procedural programming, while mysqli in object-oriented.
     
    Rainulf, Oct 20, 2011 IP
  6. P1raten

    P1raten Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    Fine, mysqli is more safe. Happy?
     
    P1raten, Oct 20, 2011 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Depending on your database, you might want to connect/use/close every time you need a database access. Too many open connections slow a database down, and crashing with an open connection could cause you to lose database access. (This is true for any database, not just MySQL, and for any connection method.) The actual overhead of opening and closing the connection is tiny compared to the time it takes to transfer the data to or from the user.
     
    Rukbat, Oct 21, 2011 IP