View Single Post
  #12 (permalink)  
Old 03-01-08, 08:59
RichardM RichardM is offline
BOD Member
 
Join Date: Jan 2008
Location: NYC
Posts: 88
Default

Quote:
Originally Posted by Christina View Post
Once the Database will be closed the script ends automatically. To close the connection before, use the mysql_close() function.

HTML Code:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code

mysql_close($con);
?>
Also:

PHP Code:
mysql_close() 
without the identifier in the (), will close the most recently open connection.

As Christina said, it usually is unnecessary to explicitly close a connection, but there are times when it is necessary.

For example, when multiple connections are needed (for example, when the same script needs to query one database to get certain information, and then query a different database to get additional information), it may be necessary to explicitly close the first connection before opening a second or subsequent connection. In this case, it's critical to use the correct identifier, for example

PHP Code:
mysql_close($first_connection); 
because mysql_close() without the identifier will close the most recent open connection.

Another time when you may want to explicitly close a connection is when the script has a lot of processing to do after the information has been retrieved, and you don't want to needlessly keep the connection open while waiting for the long script to execute.

Richard
Reply With Quote