Lunarpages Web Hosting Forum

Scripting Languages Hosting Help => PHP and MySQL Support => Topic started by: 6string on June 14, 2004, 02:59:12 AM

Title: Question regarding database connect/disconnect
Post by: 6string on June 14, 2004, 02:59:12 AM
Greets all,

A quick question regarding mysql.  I have written the following function to retrieve field data from my db.

function dlcount ($file) {
   $con = db_connect();
   $sql = "SELECT * FROM files WHERE nFileID = " .$file;
   $result = mysql_query($sql,$con);
   $row = mysql_fetch_assoc($result);
   $count = $row["nCount"];
   echo $count;
}

My question is in regards to database connection.  Do I need to/should I disconnect from the database at the end of this function?  I am new to this stuff, just wondering how this is handled.  Any input is appreciated.
Title: Question regarding database connect/disconnect
Post by: TranzNDance on June 14, 2004, 03:16:28 AM
Code: [Select]
mysql_close();
is optional because PHP automatically closes the connection at the end of the script, but it's a good habit to close whatever you open. :)
Title: Question regarding database connect/disconnect
Post by: 6string on June 14, 2004, 03:36:21 AM
Hey TranzNDance,

Thanks again for helping out and for the quick reply/info.  As I'm just digging into this, I would like to make a valiant effort to develop good habits with it as you said.

One last question on db closing, would a function like the following essentially do the same?  The built in function you offered is easier, but I'm just wondering for future ref.  I was thinking if I created one PHP file with a bunch of functions, then just used an include statement for this file in other files (like the one including the code in first post), it might save coding if I had many files using the same functions.  

function db_disconnect($con) {
   close($con);
}
Title: Question regarding database connect/disconnect
Post by: patrickdepinguin on June 14, 2004, 12:16:55 PM
it would if you use mysql_close (which was probably a typo I suppose)

There is more info here:
http://www.php.net/mysql_close
Title: Question regarding database connect/disconnect
Post by: TranzNDance on June 14, 2004, 12:21:59 PM
Quote from: 6string
Hey TranzNDance,

Thanks again for helping out and for the quick reply/info.  As I'm just digging into this, I would like to make a valiant effort to develop good habits with it as you said.

One last question on db closing, would a function like the following essentially do the same?  The built in function you offered is easier, but I'm just wondering for future ref.  I was thinking if I created one PHP file with a bunch of functions, then just used an include statement for this file in other files (like the one including the code in first post), it might save coding if I had many files using the same functions.  

function db_disconnect($con) {
   close($con);
}

I'm not sure... is this function supposed to do more than the built-in one? Otherwise, you're just adding excess overhead. :?