这是一道古老的题目
请看代码,数据库关闭指令将关闭哪个连接标识?( )
$link1 =mysql_connect("localhost","root",""); $link2 = mysql_connect("localhost","root",""); mysql_close();
A.$link1
B.$link2
C.全部关闭
D.报错
根据知识储备,mysql_close在未指定连接标识时,是就近原则。
手册上也有说明
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.
所以答案应该是B。
再想想,似乎有哪里漏了,
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
那么两次connect打开的应该是同一个连接标识,那么应该是一起关闭了吧?
所以选C???
没错,我就是闲的蛋疼,要挣扎着纠结这一个将要被淘汰的函数。于是又验证了一下。
$link1 = mysql_connect('localhost','root',''); $link2 = mysql_connect('localhost','root',''); print_r($link1); print_r($link2);//$link1和$link2一样,第二次未创建新连接,返回已经打开的连接标识 mysql_close(); //下面可以正常打test库里表都打印出来 mysql_select_db('test',$link2); $rs = mysql_query('show tables;',$link2); while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "<br />"; } //这个当然也能 mysql_select_db('test',$link1); $rs = mysql_query('show tables;',$link1); while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "<br />"; }
没错,mysql_close()谁也没关闭。
实际上,如果把mysql_close();修改为
<code>mysql_close($link1); 或者 mysql_close($link2);</code>
下面$link1,$link2仍然都是有效的
如果真要close,只有这样才好使,
mysql_close($link1); mysql_close($link2);
两个一起close。
这是为什么呢?为什么呢?
回复内容:
这是一道古老的题目
请看代码,数据库关闭指令将关闭哪个连接标识?( )
$link1 =mysql_connect("localhost","root",""); $link2 = mysql_connect("localhost","root",""); mysql_close();
A.$link1
B.$link2
C.全部关闭
D.报错
根据知识储备,mysql_close在未指定连接标识时,是就近原则。
手册上也有说明
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.
所以答案应该是B。
再想想,似乎有哪里漏了,
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
那么两次connect打开的应该是同一个连接标识,那么应该是一起关闭了吧?
所以选C???
没错,我就是闲的蛋疼,要挣扎着纠结这一个将要被淘汰的函数。于是又验证了一下。
$link1 = mysql_connect('localhost','root',''); $link2 = mysql_connect('localhost','root',''); print_r($link1); print_r($link2);//$link1和$link2一样,第二次未创建新连接,返回已经打开的连接标识 mysql_close(); //下面可以正常打test库里表都打印出来 mysql_select_db('test',$link2); $rs = mysql_query('show tables;',$link2); while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "<br />"; } //这个当然也能 mysql_select_db('test',$link1); $rs = mysql_query('show tables;',$link1); while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "<br />"; }
没错,mysql_close()谁也没关闭。
实际上,如果把mysql_close();修改为
<code>mysql_close($link1); 或者 mysql_close($link2);</code>
下面$link1,$link2仍然都是有效的
如果真要close,只有这样才好使,
mysql_close($link1); mysql_close($link2);
两个一起close。
这是为什么呢?为什么呢?
楼主的测试很赞,确实如此,mysql_connect返回的肯定是一个引用,如楼主的测试说明该句柄不但有一个变量引用而且还有一个句柄引用(我自己随便命名的),mysql_close的时候释放句柄引用,然后检查该变量的所有句柄引用是不是都被释放了,如果是的话则对该变量进行内存回收处理。例如
<code>$a = 'Hello'; $b = &$a; $c = &$a; unset($a); echo $b . $c; //HelloHello 而不是 null</code>
这个测试代码说明了内存回收关于引用的处理,所以推测mysql_close也是类似的处理,但是与unset的管理目标是不同的(例如unset管理的是内存指针,mysql_close管理的是连接句柄指针?)
不过貌似研究这个已经完全没有价值了,相同的事情在mysqli系列函数上不会发生:
<code><?php $link1 = mysqli_connect('localhost', 'root'); $link2 = mysqli_connect('localhost', 'root'); mysqli_close($link1); print_r(mysqli_get_host_info($link1)); print_r(mysqli_get_host_info($link2));</code></code>
而且php5已经抛弃了mysql系列函数,因此就算是bug或者不良问题也不会被修复了,调用mysql_connect时应该能看到警告信息:该系列函数将会在未来停止使用。
感谢@incNick 回答,已采纳答案。
另外补充一些内容
就算只有有一个连接的情况
$link1 = mysql_connect('localhost','root',''); $result = mysql_close(); var_dump($result); mysql_select_db('test',$link1 ); $rs = mysql_query('show tables;',$link1 ); while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "<br />"; }
mysql_close()在未指定link_identifier的时候,返回结果是true,
Returns TRUE on success or FALSE on failure.
表示关闭成功,但实际其实也是没有关闭的,依然能依次打印库里的表,该连接可以使用。
所以说有时间手册也挺害人的。
mysqli_close(),在未指定link_identifier的情况下执行结果是false。
在多个连接的情况下,在指定link_identifier的情况下,可单独关闭$link1,$link2。
综上,link_identifier是必需的!!!!
现在不是用pdo,mysqli了么,mysql_connect 之类的不鼓励使用了

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

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.
