Why do we have to close the MySQL database after a query command?

UserMat picture UserMat · Oct 15, 2013 · Viewed 13.3k times · Source

I'm starter.

I want to know what will happen if we don't close the MySQL connection.

1- Is it possible to open more than one database if we don't close them? I mean can we open more than one database in a same time?

2- Does closing database increase the speed?

3- Is it necessary to close the database or it is optional?

Look at this code. I don't use "mysql_close()" so I don't close the database after each request. There are a lot of requests for this PHP page. Maybe 50000 per each minute. I want to know closing database is necessary for this code or no?

<?php   
//Include the file that lets us to connect to the database.
include("database/connection.php");

//Call "connect" function to connect to the database.
connect("database", "localhost", "root", "", "user");

//The GPRS module send a string to this site by GET method. The GPRS user a variable named variable to send the string with.
$received_string = $_GET["variable"];

//Seprates data in an array.
$array_GPRS_data = explode(",", $received_string);

//we need to remove the first letter.
$array_GPRS_data[9] = substr($array_GPRS_data[9], 1);


$array_GPRS_data[13] = substr($array_GPRS_data[13], 4, 2).substr($array_GPRS_data[13], 2, 2).substr($array_GPRS_data[13], 0, 2);

//Query statement.
$query = "INSERT INTO $array_GPRS_data[17](signal_quality, balance, satellite_derived_time, satellite_fix_status, latitude_decimal_degrees,
latitude_hemisphere, longitude_decimal_degrees, longitude_hemisphere, speed, bearing, UTCdate, theChecksum)
VALUES('$array_GPRS_data[0]', '$array_GPRS_data[1]', '$array_GPRS_data[5]', '$array_GPRS_data[6]', '$array_GPRS_data[7]',
'$array_GPRS_data[8]', '$array_GPRS_data[9]', '$array_GPRS_data[10]', '$array_GPRS_data[11]', '$array_GPRS_data[12]', '$array_GPRS_data[13]',
'$array_GPRS_data[16]')";

//Run query.
$result = mysqli_query($query);

//Check if data are inserted in the database correctly.
if($result)
{
    echo("*#01");
}
else
{
    echo("Error: 001");
    echo (mysqli_error());
}   
?>

Answer

Hamza Kubba picture Hamza Kubba · Oct 15, 2013
  1. Yes, you can have multiple database connections. You are not opening a database, you are opening a database connection. The database is 'open' (i.e. running) all of the time, generally speaking, whether you are connected to it or not.
  2. Depends... if you only have one open connection on a page, then you don't need to close it because it will automatically close when PHP is done. If you have many, then you could potentially make the database server slower, or make the database server run out of available connections (it can only have a certain number of connections open at the same time). That said, most modern database servers can handle hundreds of concurrent connections.
  3. Optional, but recommended. It's not a big deal for small-medium projects (i.e. if you have less than 100 concurrent visitors at any given time, you probably won't have any issues regardless). Since you have many thousand visitors per minute, you should actively close the database connection as soon as you are done with it, to free it up as soon as possible.