为什么我们必须在执行查询命令后关闭MySQL数据库? [英] Why do we have to close the MySQL database after a query command?

查看:398
本文介绍了为什么我们必须在执行查询命令后关闭MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者.

我想知道如果不关闭MySQL连接会发生什么情况.

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

1-如果不关闭数据库,是否可以打开多个数据库?我的意思是我们可以同时打开多个数据库吗?

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-关闭数据库是否会提高速度?

2- Does closing database increase the speed?

3-是否需要关闭数据库或它是可选的?

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

查看此代码.我不使用"mysql_close()",所以在每个请求之后我都不会关闭数据库.此PHP页面有很多要求.也许每分钟5万.我想知道关闭此代码是否需要数据库?

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());
}   
?>

推荐答案

  1. 是的,您可以有多个数据库连接.您不是在打开数据库,而是在打开数据库连接.一般来说,无论您是否连接到数据库,数据库始终处于打开"状态(即正在运行).
  2. 取决于...如果页面上只有一个打开的连接,则不需要关闭它,因为在PHP完成后它将自动关闭.如果有很多服务器,则可能会使数据库服务器的速度变慢,或者使数据库服务器的可用连接用尽(它只能同时打开一定数量的连接).也就是说,大多数现代数据库服务器可以处理数百个并发连接.
  3. 可选,但建议使用.对于中小型项目而言,这并不是什么大问题(即,如果在任何给定时间同时访问的访客少于100个,那么无论如何您都不会有任何问题).由于每分钟有数千名访问者,因此您应该在完成连接后立即主动关闭数据库连接,以尽快释放它.

这篇关于为什么我们必须在执行查询命令后关闭MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆