Does mysql_connect require mysql_close and automatic release? how can this problem be solved?

Source: Internet
Author: User
Tags php database
After mysql_connect, do you need mysql_close? are you sure you want to release the PHP database automatically? is the PHP database connection released after the page is executed? can it be released even if mysql_close () is not used? Similarly, after PDO is connected to the database, do you have to assign a value of NULL to the PDO instance to disconnect the PDO connection? I checked the information long before N, but after mysql_connect is created, does it have to be mysql_close? is it automatically released?
Everyone, are PHP database connections released after the page is executed, even if mysql_close () is not used?
Similarly, after PDO is connected to the database, do you have to assign a value of NULL to the PDO instance to disconnect the PDO connection?

I checked the information N a long time ago, but it is vague. it is said that when PHP processes every requested page, there is a MYSQL connection in it, after the page is processed, all the required materials will be released immediately. For example, the connection after mysql_connect. Is that true? Is PDO the same?

------ Solution --------------------
The following is an excerpt from the PHP manual:
Mysql_close () closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn' t specified, the last opened link is used.

Using mysql_close () isn' t usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.

Mysql_close is not required for non-permanent connect.
------ Solution --------------------
Mysql_connect does not need to be forcibly closed with mysql_close
Go to the php Manual.
As mentioned above, the mysql_connect () function opens a non-persistent MySQL connection. Note: When the script ends, the connection to the server is closed, unless mysql_close () has been explicitly called before.
That is, when the script ends, it is automatically disabled.
------ Solution --------------------
If mysql_pconnect is different, you need to close it with mysql_close.
------ Solution --------------------
Generally, mysql_close () is not required, because a non-persistent connection is automatically closed after the script is executed,
------ Solution --------------------
Discussion

If mysql_pconnect is different, you need to close it with mysql_close.

------ Solution --------------------
Why does the SQL Server automatically close the connection opened by mysql_connect after the script is executed?

This is yes

The duration of the long connection is 8 hours. in 8 hours, if you have not performed any operation, then your connection will be released.

Wait_timeout = 28800
------ Solution --------------------
In CLI mode, whether mysql_connect or mysql_pconnect, the connection is closed as long as the script exits.

In CGI mode, the connection established by mysql_connect can be closed by mysql_close or automatically released after the request ends. However, the connection established by mysql_pconnect cannot be closed by mysql_close and will not be released after the request ends.

When mysql_pconnect is disabled, I know that:
1. the idle time exceeds the wait_timeout value of mysqld (unit: second)
2. the web server process Exits. taking nginx + php-fpm as an example, a php-fpm process ends.
------ Solution --------------------
The manual is more authoritative.

Http://cn.php.net/manual/zh/pdo.connections.php

Upon successful connection to the database, an instance of the PDO class is returned to your script. the connection remains active for the lifetime of that PDO object. to close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted -- you do this by assigning NULL to the variable that holds the object. if you don't do this explicitly, PHP will automatically close the connection when your script ends.
------ Solution --------------------
I studied mysql_pconnect when I was bored last year. I wrote a blog post carefully. you can check it out.
Http://www.cnblogs.com/funlake/archive/2011/09/08/2171822.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.