Closing a SELECT Query

Discussion in 'Databases' started by Twity, Oct 27, 2009.

  1. #1
    Do I need to close a SELECT QUERY (let say after a loop)?
     
    Twity, Oct 27, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I don't know that you are asking the right question--queries don't get closed---they execute, so there is nothing to close. You can close a database connections though.

    Post some code and what your worries about it are.
     
    plog, Oct 27, 2009 IP
  3. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #3
    there are two ways to connect to mysql.

    1. mysql_connect
    such connections automatically gets disconnected once script execution is over.
    2. mysql_pconnect
    such connections are kept live until time out.

    when you have to execute many queries in loop, its better to use mysql_pconnect before the loop and use mysql_close after loop is over.

    it is always good to explicitly close connections - be it mysql or file once its usage is over :) cleaning up garbage always helps us next time :D
     
    mastermunj, Oct 27, 2009 IP
  4. Twity

    Twity Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How can I close this?

    $result = mysql_query("SELECT * FROM my_table WHERE status = 'ABC'") or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
    While LOOP Here
    }
     
    Twity, Oct 27, 2009 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    after loop gets over, simply call mysql_close();
     
    mastermunj, Oct 27, 2009 IP
  6. Twity

    Twity Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Is this correct?

    $result = mysql_query("SELECT * FROM my_table WHERE status = 'ABC'") or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
    While LOOP Here
    }
    mysql_close();
     
    Twity, Oct 28, 2009 IP