Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Junior Member
    Join Date
    Jun 2006
    Posts
    5
    Member #
    13392
    Hi, I just started using php recently. I am using the mysql_connect() function multiple times in my php code and according to the php site:

    http://ca.php.net/manual/en/function.mysql-connect.php :
    "Opens or reuses a connection to a MySQL server."

    So, according to this I guess it is ok to call the function multiple times and reuse the same link. But a couple of times, I got an error when loading my site, which said something like:
    "too many connection to mysql" or something similar.

    Would anyone know if my approach is ok or should I use one link or what the hell?

    Because it looks like it is opening a new connection everytime I call this function. :speechless:

    Thanks!

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Your best bet is to open one connection per page. Why do you need more than one?

  4. #3
    Junior Member
    Join Date
    Jun 2006
    Posts
    5
    Member #
    13392
    I don't know, I was connecting to the database for every function that did something to my database.

    So if I do:

    $mysqllink = mysql_connect($hostname, $mysqlname, $mysqlpassword)
    mysql_select_db( $database, $mysqllink);

    and then if I'm in a function like say display_news()
    I can do a mysql_query() and it knows what to use?

    Right now, for every function, I connect to it and then do the mysql_query. But I guess that's wrong because you only need 1 connection right?

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    I can't remember for sure if it'll automatically use the appropriate connection; however, I never used it that way. Instead, there are two solutions. The first one is that you pass the $mysqllink variable in to the method. The other is that, in the function, you declare `global $mysqllink;', which tells the function to look in the global namespace for that variable. Then passing that variable in should work.

  6. #5
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Either just mysql_connect before any functions that use mysql need a connection, or just in every connection do mysql_close() but the second option is pointless IMO

  7. #6
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    It's horrendously inefficient to open any resource, be it a file, network socket, or database connection, do your stuff, and then close the connection many times per second. Open the connection for your first query, and close it with your last. Opening on the first connection is easy. It will automatically close for you when the page is done generating, but you can make a function and register it with register_shutdown_function() to do extra cleanup when your page is done.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  8. #7
    Junior Member
    Join Date
    Jun 2006
    Posts
    5
    Member #
    13392
    Yeah, I noticed more slowdown as I added more functions which did mysql queries and consequently more mysql_connects...

    Anyway, thanks! I will call mysql_connect() only once.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:17 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com