[英]Why do we have to close the MySQL database after a query command?
I'm starter. 我是首发。
I want to know what will happen if we don't close the MySQL connection. 我想知道如果我们不关闭MySQL连接会发生什么。
1- Is it possible to open more than one database if we don't close them? 1-如果我们不关闭它们,是否可以打开多个数据库? I mean can we open more than one database in a same time?
我的意思是我们可以同时打开多个数据库吗?
2- Does closing database increase the speed? 2-关闭数据库是否会提高速度?
3- Is it necessary to close the database or it is optional? 3-是否需要关闭数据库或它是可选的?
Look at this code. 看看这段代码。 I don't use "mysql_close()" so I don't close the database after each request.
我不使用“mysql_close()”所以我不会在每次请求后关闭数据库。 There are a lot of requests for this PHP page.
这个PHP页面有很多请求。 Maybe 50000 per each minute.
也许每分钟50000。 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());
}
?>
Once you connect to the database it is not necessary to close. 连接到数据库后,无需关闭。 As non-persistent connection automatically closed at the end of script execution.
由于非持久连接在脚本执行结束时自动关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.