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

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
