Cleaning up server resources after using MySQL

Discussion in 'PHP' started by tarponkeith, Oct 27, 2007.

  1. #1
    In ASP you would use:

    
    RecordSet.Close
    DataBaseConnection.Close
    Set RecordSet = Nothing
    Set DataBaseConnection = Nothing
    
    Code (markup):
    Is there anything you need to do with MySQL to clean up resources?

    From what I've seen, it looks like:
    mysql_close();
    Code (markup):
    is the only thing used to "clean up"...

    
    
    <?php
    $connection = mysql_connect("localhost", "user", "pass") or die (mysql_error());
    mysql_select_db("db_table");
    
    $getalldata = mysql_query('select * tablename') or die (mysql_error());
    $numrecords = mysql_num_rows($getalldata);
    
    if($numrecords == '0')
    {
    	echo "Nothing!  Sorry!";
    }
    else
    {
    	while ($data = mysql_fetch_array($getalldata))
    	{
    		$field_title = $data['field_title'];
    		$Joke_Text = stripslashes($field_title);
    	}
    }
    mysql_close($connection);
    ?>
    
    
    Code (markup):
    Thanks
     
    tarponkeith, Oct 27, 2007 IP
  2. Fash

    Fash Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    As far as I know (though I could be wrong), mysql_close() is pretty much it.
     
    Fash, Oct 27, 2007 IP
  3. gota

    gota Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If your concern with using resources, then you should think about everything you do. Resource handles are never a problem, it's what they reference is what causes unneeded resource waste!

    Take for example...

    $getalldata = mysql_query('select * tablename') or die (mysql_error());
    Code (markup):
    The use of (SELECT *), is very bad by design. One should never use it, unless you are actually using all columns in the result set. Which doesn't happen very much. Your only using column field_title, so that should be the only thing in the SELECT statement.

    Then below variable assignment isn't needed, because there isn't a need for it, your just wasting allocated memory space for nothing. Sure it's not a problem with a site that is not busy, but if you have a very busy site, that 84 bytes used to hold that unneeded variable can be costly. Especially if your doing that many different times in a single script execution.

    $numrecords = mysql_num_rows($getalldata);
    Code (markup):

    The the below code makes no sense. Copying variables is a no no, it completely goes against all logical programming. It's not needed, so why hold (2) variables that contain the exact same value.

    		$field_title = $data['field_title'];
    		$Joke_Text = stripslashes($field_title);
    Code (markup):

    Programming is not just about getting something done, that you need to do. The equation that gets what you need done is always the real answer to writing logical, resource friendly code!

    Also if your running more code logic after the while() loop, always free the result resource, there is no sense to keep that memory allocated with your done using it! See mysql_free_result();, for more info!


    This post in no way was meant to criticize your code, it was only to say that resource auditing is a critical part of any applications development logic, so everything within the script should audited and evaluated for it's logical resource usage and handling.
     
    gota, Oct 27, 2007 IP