Close connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Close connection

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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? :?:
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_close()

:roll:
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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);
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

OK. Thanx guys for your help. :)
Post Reply