chiprivers Posted December 23, 2006 Share Posted December 23, 2006 Is there a way of checking for a open connection to database that I can put in an if statement?ie if ( ! is there a connection ) { // no connection so must connect mysql_connect(); } Link to comment https://forums.phpfreaks.com/topic/31711-check-for-connection/ Share on other sites More sharing options...
.josh Posted December 23, 2006 Share Posted December 23, 2006 I usually assign connections to variables. If you do that, then you can check if the variable is set.[code]$conn = mysql_connect('host','user','pass') or die(mysql_error());...if (!$conn) { // ...}[/code] Link to comment https://forums.phpfreaks.com/topic/31711-check-for-connection/#findComment-146974 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 "Opens or reuses a connection to a MySQL server."This makes me think if there is already a connection open, it will use that one. So you don't really need to check...I always assign to a variable also. I wrap my database connection in a function for error handling, etc, and use pconnect. Link to comment https://forums.phpfreaks.com/topic/31711-check-for-connection/#findComment-146976 Share on other sites More sharing options...
chiprivers Posted December 23, 2006 Author Share Posted December 23, 2006 The reason I am asking is that I am creating a group of functions that will extract and display information from my database. I though it would be a good idea to check at the beginning of the function if there was an exisiting connection, else open a connection. Link to comment https://forums.phpfreaks.com/topic/31711-check-for-connection/#findComment-146978 Share on other sites More sharing options...
.josh Posted December 23, 2006 Share Posted December 23, 2006 building on jesirose's interpretation: From the manual:[quote]If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.[/quote]so if you were to just do mysql_connect() in every one of your functions (or call a connection function with error handling, as jesirose suggested (which is what i do too)) , it will do what you want it to: open a new connection, or do nothing if it already exists Link to comment https://forums.phpfreaks.com/topic/31711-check-for-connection/#findComment-146981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.