Hioushi Posted May 25, 2010 Share Posted May 25, 2010 I have a class to handle DB communication doing something like $sql = new Sql(); $sql->query($foo); It handles a few things I need before and after queries and also handles results the way I need it. My question is, I was thinking about putting a mysql_connect() and mysql_close() inside the query function, for convenience, would that be wrong? The server will be handling 10 to 20 connections tops, in case that's relevant. Thank you. Link to comment https://forums.phpfreaks.com/topic/202866-using-mysql_connect-before-each-query/ Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 Put it inside the class constructor, not the query statement. Opening and closing mysql connections every time a query is ran will cause an unneeded burden on your server. Infact you do not need to call mysql_close at all. As the connection is automatically closed by php after processing. But I would highly suggest against putting the connection string and opening / closing connections for each query. It is unnecessary and will cause more trouble then it is worth. Link to comment https://forums.phpfreaks.com/topic/202866-using-mysql_connect-before-each-query/#findComment-1063145 Share on other sites More sharing options...
Hioushi Posted May 25, 2010 Author Share Posted May 25, 2010 I see, thank you, that does seem like a better approach. In a related note, is it safe to leave a DB connection open (I know it has a timeout), but are there any security implications in doing so? Link to comment https://forums.phpfreaks.com/topic/202866-using-mysql_connect-before-each-query/#findComment-1063161 Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 php actively closes the connection on script ending. It does not timeout unless the script timelimit is set to 0 (infinite) and then it may timeout. But yea, php is smart enough to close the connection for you. Link to comment https://forums.phpfreaks.com/topic/202866-using-mysql_connect-before-each-query/#findComment-1063162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.