pouncer Posted April 9, 2009 Share Posted April 9, 2009 I've got this code at the to of my index.php <? require_once("database.php"); require_once("functions.php"); $db = new Database(); $db->Connect(); ?> The Connect() method is just the mysql_connect(...) stuff. So everytime I refresh my index.php, is my web app always executing the mysql_connect?? is there a better way to do it so it just connects once when i open the index.php and not again and again... Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/ Share on other sites More sharing options...
Mark Baker Posted April 9, 2009 Share Posted April 9, 2009 Use pconnect rather than connect? Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805401 Share on other sites More sharing options...
wildteen88 Posted April 9, 2009 Share Posted April 9, 2009 Yes everytime you refresh the page mysql_connect will be called. However mysql_connect will not establish a new connection if there is an existing one. Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805402 Share on other sites More sharing options...
pouncer Posted April 9, 2009 Author Share Posted April 9, 2009 Ah, I never knew about pconnect!! Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805403 Share on other sites More sharing options...
PFMaBiSmAd Posted April 9, 2009 Share Posted April 9, 2009 pconnect only works for a server module version of PHP. Most hosting runs php as a CGI application and pconnect does not work. Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805477 Share on other sites More sharing options...
revraz Posted April 9, 2009 Share Posted April 9, 2009 I wouldn't worry too much about it, because when the page is finished executing, it closes the connection anyways. Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805480 Share on other sites More sharing options...
redarrow Posted April 9, 2009 Share Posted April 9, 2009 just use, require_once("database_page.php"); Link to comment https://forums.phpfreaks.com/topic/153307-i-only-want-to-connect-to-database-once/#findComment-805481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.