pascal_22 Posted August 12, 2013 Share Posted August 12, 2013 Hi!! For now, i use mysql_..... to query the DB. Not i want to switch to mySQLi. 99% of time i not close the connection. the structure of my file are: include for header,include for footer,include for menu, include for top menu........ So i query database in all include file or almost.... so i suppose that it's not good to open connection, query db,and then close it, after in another function or include file, i open it again, query the DB, and close it again...... and so on... Is it the best to open it in header..... and close in in footer page...? Or is there a better way? For performance! Thanks a lot! Pascal Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/ Share on other sites More sharing options...
mac_gyver Posted August 12, 2013 Share Posted August 12, 2013 I'm kind of wondering if you are including these pieces of your page through the file system or via a http request. through the file system would use one database connection. through a http request would use a separate database connection for each include. what does one of these include statements look like? Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444596 Share on other sites More sharing options...
pascal_22 Posted August 12, 2013 Author Share Posted August 12, 2013 (edited) Thanks for your reply! here an exemple of index.php <html> <?php echo config.php?> //in config.php i update/insert onlinemembers, i get location from ip <head><?php echo includes_css.php?></head> <body> <div><?php echo topmenu.php?></div> <div><?php echo leftmenu.php?></div> <?php get data from db.... ?> <div><?php echo footer.php?></div> </body> </html> and don't know if it answer to your question? thanks a lot Edited August 12, 2013 by pascal_22 Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444604 Share on other sites More sharing options...
mac_gyver Posted August 12, 2013 Share Posted August 12, 2013 what you have posted above is not valid php code and it doesn't show how you are including anything. if you altered the code for the post because it contained links in the include statements that showed your domain, i.e. include 'http://your_domain.com/topmenu.php';, then this means that you are including the pieces of your page using http requests and each of those is making its own separate database connection that counts toward the connection limit. to get actual help with what you are doing you must post actual code. ******* out your domain name if you don't want to post it, but post what you are actually doing in your code. Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444612 Share on other sites More sharing options...
pascal_22 Posted August 12, 2013 Author Share Posted August 12, 2013 Yeah! Sorry i saw that i made a mistake... here an exemple of my index <?php session_start(); include 'required-all-pages.php'; ?> <!doctype html> <head><?php include 'include_head_tag.php';?> <?php include 'include_css.php';?> </head> <body><?php include 'alertesmessagesbox.php';?> <div id="main-container"> <header> <?php include 'topmenu.php';?> <?php include 'logo.php';?> </header> <div id="content-wrapper"> <?php include 'menu.php';?> <div id="content" role="main"> <?php include 'i-recentlyonline-big.php';?> <br/> <?php include 'i-inscription.php'; ?> <?php include 'ForumsAlertesNews.php'; ?> //other content here! </div> </div> <?php include 'left-menu.php'; ?> <?php include 'footer.php'; ?> </div> </body> </html> There it is! I delete title tag, desc tag...... some things thanks! Hope this is ok! Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444616 Share on other sites More sharing options...
pascal_22 Posted August 12, 2013 Author Share Posted August 12, 2013 So as you can see, in some include, i need a db connection. the page required_all_pages.php, is for creating session vars, get user location by ip, update/insert online members.......... So what sould i do.. open it at the beginnig of required_all_pages.php and close it in footer.php? thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444618 Share on other sites More sharing options...
pascal_22 Posted August 12, 2013 Author Share Posted August 12, 2013 also, in my include, i do not insert my domain name. What i do is: include 'login.php'; and NOT include 'http://www.mydomainname/login.php'; thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444619 Share on other sites More sharing options...
Solution kicken Posted August 12, 2013 Solution Share Posted August 12, 2013 Generally you open the connection in some common include file which is used on all pages. Based on your file names, I am going to assume that required-all-pages.php fits that description so that is where you would open your database connection. As for closing it, you don't. Let PHP handle that at the end of your script. With a simple include setup like that, variables will be available across includes so if you simply do: $dbcon = new mysqli(...); inside of the required-all-pages.php file, then in all your other files you can query the database by just accessing $dbcon. Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444623 Share on other sites More sharing options...
pascal_22 Posted August 12, 2013 Author Share Posted August 12, 2013 (edited) Ok perfect! And for function... if i call a fonction... function TEst{ //query the DB } Will $dbcon be available? I think i should put global $dbcon; ? right? thanks! Edited August 12, 2013 by pascal_22 Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444625 Share on other sites More sharing options...
kicken Posted August 12, 2013 Share Posted August 12, 2013 You should pass the connection in as a parameter. function test($dbcon /*, other args */){ //... } test($dbcon); Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444633 Share on other sites More sharing options...
pascal_22 Posted August 13, 2013 Author Share Posted August 13, 2013 Ok perfect!! Thanks!! and the use of global.. is it the same.... function test() { global $dbcon; //...} thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444672 Share on other sites More sharing options...
trq Posted August 13, 2013 Share Posted August 13, 2013 Don't use the global keyword, do as kicken suggested and pass $dbconn in as a param. Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444691 Share on other sites More sharing options...
pascal_22 Posted August 13, 2013 Author Share Posted August 13, 2013 Thanks a lot!!! That's what i do!! Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444763 Share on other sites More sharing options...
mac_gyver Posted August 13, 2013 Share Posted August 13, 2013 this isn't the first time someone has asked about a too many connection error with code that opens a connection/runs a query/closes a connection, repeated more than once in a script... i am wondering if when you close a connection in php how much time it actually takes to send that commend to the database server and for the connection/process to actually be shut down on the database server. a possibility - php considers the connection closed when the _close() statement is executed, so that the next connection in the script will attempt to create a new connection to the database server. the database server sees a request for a connection and goes through the process to make a new connection/start a new process to service the connection, but perhaps the previous connection is still in the process of being destroyed/shutdown on the database server. a possible end result - a script doing this could actually be consuming multiple database connections, triggering a too many connection error because each concurrent instance of the script is tying up more than one actual connection/process on the database server. Quote Link to comment https://forums.phpfreaks.com/topic/281089-when-closing-connection-when-using-it-a-lot-in-page/#findComment-1444767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.