How to close connection of PHP ???

Discussion in 'PHP' started by mahdi_fci3, Sep 26, 2010.

  1. #1
    Hi all,
    I always see that every large code has not a closed connection, is it mean when I have a small code I will close and when I have a large code I will not close the connection ?
    here is one of my codes, so how to close, because my connection is in a file called connection.php .
     
      <?php 
           include("../connection.php");
           $DeptID =htmlentities(mysql_real_escape_string($_POST[Dept_ID]));
           $query = "SELECT * from departement where deptID= '$OpID'     "   ;		
           $result  = mysql_query($query) ;
      ?>
    <table width="655" border="0" align="center" cellpadding="0" cellspacing="0">
       <tr>
        <td><strong>Dept ID </strong></td>
        <td><strong>Dept Name  </strong></td>
        <td><strong>Location  </strong></td>
      </tr>
    <?php    while($rows= mysql_fetch_array($result)) { ?>
        <tr>
        <td>  <?php echo  $rows[0] ;?>&nbsp;</td>
        <td>  <?php echo  $rows[1] ;?>&nbsp;</td>
        <td>  <?php echo  $rows[2] ;?>&nbsp;</td>
      </tr>
    <?php } ?>
    </table>
     
    Code (markup):
    I ask this Question because I sometimes see in my web page Error of connection " To many connection ". I dont know the cause ? so plz help me , thank u 4 ur Time.
     
    mahdi_fci3, Sep 26, 2010 IP
  2. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    Hi!

    Use This In The End!

    <?php mysql_close(); ?>
     
    HungryMinds, Sep 26, 2010 IP
  3. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks HungryMinds
    Is that means use mysql_close() at every peace of code ?
     
    mahdi_fci3, Sep 26, 2010 IP
  4. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Anytime My Friend...
    Yes...
    If U'll Not Close Your Connection...
    Some One Genius Can Hack Your CODE Easily :)
    So Close Your Connection When U Feel That Your PHP Work Has Been DONE On The Page.
     
    HungryMinds, Sep 26, 2010 IP
  5. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #5
    As far as i know, mysql automatically closes connection on the end of your script although you do not put "mysql_close()" on your script, except your script use persistent connection (mysql_pconnect)

    Assuming your script does not use persistent connection, it means your server (actually the mysql) is not powerful enough to handle too many visitors.
     
    xrvel, Sep 26, 2010 IP
  6. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank u all my friends.
     
    mahdi_fci3, Sep 27, 2010 IP