mds1256 Posted October 18, 2010 Share Posted October 18, 2010 I have created a function to connect to a required database with a username and password. I have called this db_openConnection() Now for each function that requires a select from a database is it ok just to call the db_OpenConnection() first within the function or should i be keeping open a db connection (using persist, i think) rather than keep creating a connection. I know that the connection ends when you have finished a query any how. Not sure if this is the correct forum or if it should be mysql forum Link to comment https://forums.phpfreaks.com/topic/216168-connect-to-mysql-db-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2010 Share Posted October 18, 2010 I know that the connection ends when you have finished a query any how. No it doesn't. Where did you get that information from? A connection remains open until you specifically close it or until it is automatically closed when the script execution ends on the current page request. Link to comment https://forums.phpfreaks.com/topic/216168-connect-to-mysql-db-function/#findComment-1123467 Share on other sites More sharing options...
mds1256 Posted October 18, 2010 Author Share Posted October 18, 2010 I know that the connection ends when you have finished a query any how. until it is automatically closed when the script execution ends on the current page request. Sorry thats what i meant Link to comment https://forums.phpfreaks.com/topic/216168-connect-to-mysql-db-function/#findComment-1123470 Share on other sites More sharing options...
Psycho Posted October 18, 2010 Share Posted October 18, 2010 I know that the connection ends when you have finished a query any how. No, the connection ends when the script finishes. You should connect to the database once, then run all needed queries. Edit: PFMaBiSmAd beat me to that response. Rather than a function, you could create a db class. Then just have a method in the class to run a function. When that method is called the class will check if a db conneciton is already open. If so, it runs the query. If not, it opens the connection before running the query. That way a db connection is only opened if needed and you only need to open it once per page. You could do the same thing with a function, but that would require using a global variable for the db connection. But, there are many db classes freely available - no need to reinvent the wheel. Link to comment https://forums.phpfreaks.com/topic/216168-connect-to-mysql-db-function/#findComment-1123471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.