Page 1 of 1

Close connection

Posted: Wed Jan 12, 2005 10:30 pm
by S_henry
In my project, i've put the code to connect to database (open database connection) in each pages and everything is fine. But i'm quite confius the best way is either i've to put the code to close the connection in each pages or not. Anybody got idea? Thanx..

Posted: Wed Jan 12, 2005 10:35 pm
by feyd
I'm having issues with trying to understand what you're asking.

From what I can gather, you've added code to all your pages to close the database connection, and you are wondering if this is the best thing to do or not? :?:

...

Posted: Thu Jan 13, 2005 1:14 am
by Calimero
S_henry probably meant that - but anyway I wold like to know - Yes or No ( your version of interpreted question FEYD )

Thanks ahead.

Posted: Thu Jan 13, 2005 2:09 am
by S_henry
Sorry for the incomplete question. OK. Let me explain more detail. In my project, let say I have 20 php pages. For all of that 20 pages, I have to retrieve data from database. Thats mean I have to make database connection to every pages. So in all pages, I put this code at the top to make database connection:

Code: Select all

<?php

$db = mysql_connect("localhost", "", "")
      or die ("Could not connect to Database");    

mysql_select_db ("DBname",$db)        
      or die ("Could not select database");
?>
My issue is should I put the code to close the database connection at the bottom in all pages? Feel free to ask me if my issue still not clear. Thanx..

Posted: Thu Jan 13, 2005 7:53 am
by feyd
in the case of mysql, currently, closing the connection is automatic at end of page. However, I believe it's good practice to close it, just in case they decide to turn off the automatic shutdown of the connection in the future.

Closing the connection before page end can also help free up some memory needed for your code, if memory is an issue.

Posted: Thu Jan 13, 2005 9:05 am
by Bill H
Unless you are using persistent connections.:

Code: Select all

$Host = "localhost";               // server name
$User = "username";         // database param
$Password = "password";              // database param
$Link = mysql_pconnect($Host, $User, $Password);
mysql_select_db($dbmName, $Link);  // link to the main database by default
In that case you do not want to close the connection on each page (or at all, let it time out) and, depending on your host and the server configuration, this is much friendlier to the host.

Posted: Thu Jan 13, 2005 8:09 pm
by S_henry
OK. Maybe i'll try Bill H way (to make database connection) next time. But at this time, can anybody give me the code to close the database connection which i want to put at the bottom in all of my pages.

Posted: Thu Jan 13, 2005 8:26 pm
by feyd
mysql_close()

:roll:

Posted: Thu Jan 13, 2005 8:27 pm
by Bill H

Code: Select all

$Host = "localhost";               // server name
$User = "username";         // database param
$Password = "password";              // database param
$Link = mysql_connect($Host, $User, $Password);
mysql_select_db($dbmName, $Link);  // link to the main database by default
//code stuff
mysql_close($Link);

Posted: Fri Jan 14, 2005 1:15 am
by S_henry
OK. Thanx guys for your help. :)