Jump to content

When closing connection when using it a lot in page


pascal_22
Go to solution Solved by kicken,

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by pascal_22
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

  • Solution

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.