Forgot to close Recordsets at each page, Wht to do now

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
khurramfraz
Forum Newbie
Posts: 2
Joined: Mon Oct 30, 2006 11:51 pm

Forgot to close Recordsets at each page, Wht to do now

Post by khurramfraz »

We have developed very large php website, using Fusebox framework. we thought during development that all resources all autmatically closed at the end of php page. but it is not in the case of recordset/resultset objects and these objects does not release memory.

Now i want to close all recordsets in entire application , if i do it in every page it takes too much time bec too many pages.

Is there any way that all recordsets are closed when request completed. any simplest way to hadle this problem ?

Thanks in advance

Khurram Fraz
ohlaph
Forum Newbie
Posts: 7
Joined: Mon Oct 30, 2006 11:42 pm

Post by ohlaph »

There is really no quick way to close them all...
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Actually they ARE closed on later versions of PHP. If you are using a version of PHP earlier than 4.1 then there is a bug that will cause those resource connections to mysql to stay open. This bug is really noticeable if you are using persistent connections.

But the only good practice is to close all results as soon as you are finished with them just in case there is a problem. Especially if you have a large program because it will free up memory.

BTW, on most servers using persistent connections will not gain you any speed or lower overhead. They did help when servers still well below 1 ghz but there isn't any real benefit to using them anymore. The reason I mentioned this is because versions of PHP before 4.1 had a nasty bug with persistent mysql connections. You couldn't close them and often times they wouldn't be reused and they would stack up until the max connections was hit. Then that happened the mysql server would stop processing requests and a reboot would be needed.
khurramfraz
Forum Newbie
Posts: 2
Joined: Mon Oct 30, 2006 11:51 pm

Post by khurramfraz »

Dear AKA Panama Jack

Thanks for ur reply.

We are using PHP 5, so should i need to close all recodsets(how ?) or they are automatically closed in PHP version 5 ? wht do u say ?


Thanks

Khurram Fraz
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

They should be automatically closed unless another bug has been introduced to the PHP mysql code.

But DO NOT rely on PHP closing result sets for you when the program finishes executing. It is sloppy programming and can come bite you in the butt at a later date. It's best to close all result sets and the database connection before PHP finishes executing the program.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Could you post some code to illustrate the best way to close the connection properly.
Do I need to use both mysql_close and mysql_free_result?


Also, I guess it's ok to close the connection once the query result is obtained in a variable?

... Unless there's multiple queries on a page, in which case I guess just close after the last one to avoid connecting / disconnecting - I guess that would add an overhead.

I just want to make sure I'm doing this correctly!
Thanks
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Use the mysql_free_result to close the result set when you have finished pulling the data created by the query. This will free up the memory taken by the query.

Just before your php program finishes use mysql_close to close the connection between PHP and the mysql server. This will free up memory and a connection slot on the mysql server for another connection.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Ok great

Thanks for the info.
Post Reply