mysql_connect后是不是非要mysql_close,是不是自动释放,该如何解决
mysql_connect后是不是非要mysql_close,是不是自动释放
大家伙们,PHP数据库连接是不是在页面执行完之后自己释放,即使不用mysql_close()也可以释放掉?
同理PDO连接到数据库后,是不是非要将PDO实例赋值为NULL,才能将PDO连接断掉?
在N久以前我查过资料,但己经模糊不清了,具说PHP处理每一个被请求的页面时,里面有MYSQL连接的,当页面处理完就立刻释放掉所有用到的资料。如:mysql_connect后的连接。这个说法对吗?PDO同理吗?
------解决方案--------------------
以下摘自PHP手册:
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对于非永久的connect不是必须的。
------解决方案--------------------
mysql_connect不一定需要强制用mysql_close关闭的
可以去看下php手册
上面说了mysql_connect() 函数打开非持久的 MySQL 连接。注释:脚本一结束,到服务器的连接就被关闭,除非之前已经明确调用 mysql_close() 关闭了。
也就是说,当脚本结束时,会自动关闭。
------解决方案--------------------
如果是mysql_pconnect就不一样了,需要自己用mysql_close关闭
------解决方案--------------------
通常不需要使用 mysql_close(),因为已打开的非持久连接会在脚本执行完毕后自动关闭,
------解决方案--------------------
------解决方案--------------------
那mysql_connect打开的连接会在脚本执行完毕后由SQL服务器自动关闭喽?
這個是的
長鏈接的默認時效是8個小時.8個小時內你沒有操作那麼將斷開你的鏈接
wait_timeout=28800
------解决方案--------------------
CLI模式下,不论是mysql_connect还是mysql_pconnect,只要脚本退出,连接就被关闭。
CGI模式下,mysql_connect建立的连接,可以被mysql_close关闭;也可以不手工关闭,而等待请求结束后自动释放。但是mysql_pconnect建立的连接,无法被mysql_close关闭,请求结束后也不会释放。
mysql_pconnect被关闭的情况,我知道有:
1、空闲时间超过了mysqld的wait_timeout(单位是秒)
2、web server进程退出,以nginx+php-fpm为例,就是一个php-fpm进程结束
------解决方案--------------------
手册上说的比较权威
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.
------解决方案--------------------
去年无聊时研究过mysql_pconnect,认真写了篇博文,你可以看看
http://www.cnblogs.com/funlake/archive/2011/09/08/2171822.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
