SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL-Connection - to close or not?

    Hi!

    Running PHP an MySQL - to get some information from the database the sequence is as follows: First connect to the database, then send the query command, then work with the results and then close the connection.

    My question: What if I have two queries on the same page? [Connect / Send Query 1 / Work Results 1 / Close] - [Connect / Send Query 2 / Work Results 2 / Close]? Or is this putting too much load on the connection? Would [Connect / Send Query 1 / Work Results 1 / Send Query 2 /Work Results 2 / Close] be sufficient?

    What if I have two queries on the same page but different tables of the same database? Or two queries on the same page but different databases?


  2. #2
    code addict Abstraction's Avatar
    Join Date
    Apr 2001
    Location
    Des Moines, IA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would only open the connection once. If you need need to access two different databases, then you will have to connect twice.

  3. #3
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry about closing -- PHP will do that for you.

    re: 2 DBs as long as they reside on the same physical machine you can do this:
    PHP Code:
    mysql_connect"server" );

    mysql_query"SELECT * FROM db1.table" );
    mysql_query"SELECT * FROM db2.table" ); 
    That will allow you to select records from two different databases on the same machine. If they are not, you will have to make 2 connects as Abstraction suggested.

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use mysql_select_db to change databases.
    ck :: bringing chris to the masses.

  5. #5
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Anarchos
    Just use mysql_select_db to change databases.
    Eh, too much code. Same with mysql_close -- I am a minimalist I guess.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx!


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
  •