Lunarpages Web Hosting Forum

Author Topic: Question regarding database connect/disconnect  (Read 2080 times)

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Question regarding database connect/disconnect
« 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.
Regards,
Dave

Offline TranzNDance

  • Princess of Naboo
  • Berserker Poster
  • *****
  • Posts: 11567
    • Thu Tu's Blog
Question regarding database connect/disconnect
« Reply #1 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. :)
:whip: :love: :whip: :love: :whip: :love:

Offline 6string

  • Pong! (the videogame) Master
  • *****
  • Posts: 20
Question regarding database connect/disconnect
« Reply #2 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);
}
Regards,
Dave

Offline patrickdepinguin

  • Spaceship Navigator
  • *****
  • Posts: 98
Question regarding database connect/disconnect
« Reply #3 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

Offline TranzNDance

  • Princess of Naboo
  • Berserker Poster
  • *****
  • Posts: 11567
    • Thu Tu's Blog
Question regarding database connect/disconnect
« Reply #4 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. :?
:whip: :love: :whip: :love: :whip: :love: