mysql_close关闭哪个连接标识

WBOY
Release: 2016-06-06 20:11:20
Original
1565 people have browsed it

这是一道古老的题目

请看代码,数据库关闭指令将关闭哪个连接标识?( )

<code class="php">$link1 =mysql_connect("localhost","root","");
$link2 = mysql_connect("localhost","root","");
mysql_close();</code>
Copy after login
Copy after login

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???

没错,我就是闲的蛋疼,要挣扎着纠结这一个将要被淘汰的函数。于是又验证了一下。

<code class="php">$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>";
}</code>
Copy after login
Copy after login

没错,mysql_close()谁也没关闭。

实际上,如果把mysql_close();修改为

<code>mysql_close($link1);
或者
mysql_close($link2);</code>
Copy after login
Copy after login

下面$link1,$link2仍然都是有效的

如果真要close,只有这样才好使,

<code class="php">mysql_close($link1);
mysql_close($link2);</code>
Copy after login
Copy after login

两个一起close。

这是为什么呢?为什么呢?

回复内容:

这是一道古老的题目

请看代码,数据库关闭指令将关闭哪个连接标识?( )

<code class="php">$link1 =mysql_connect("localhost","root","");
$link2 = mysql_connect("localhost","root","");
mysql_close();</code>
Copy after login
Copy after login

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???

没错,我就是闲的蛋疼,要挣扎着纠结这一个将要被淘汰的函数。于是又验证了一下。

<code class="php">$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>";
}</code>
Copy after login
Copy after login

没错,mysql_close()谁也没关闭。

实际上,如果把mysql_close();修改为

<code>mysql_close($link1);
或者
mysql_close($link2);</code>
Copy after login
Copy after login

下面$link1,$link2仍然都是有效的

如果真要close,只有这样才好使,

<code class="php">mysql_close($link1);
mysql_close($link2);</code>
Copy after login
Copy after login

两个一起close。

这是为什么呢?为什么呢?

楼主的测试很赞,确实如此,mysql_connect返回的肯定是一个引用,如楼主的测试说明该句柄不但有一个变量引用而且还有一个句柄引用(我自己随便命名的),mysql_close的时候释放句柄引用,然后检查该变量的所有句柄引用是不是都被释放了,如果是的话则对该变量进行内存回收处理。例如

<code>$a = 'Hello';
$b = &$a;
$c = &$a;
unset($a);
echo $b . $c; //HelloHello 而不是 null</code>
Copy after login

这个测试代码说明了内存回收关于引用的处理,所以推测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>
Copy after login

而且php5已经抛弃了mysql系列函数,因此就算是bug或者不良问题也不会被修复了,调用mysql_connect时应该能看到警告信息:该系列函数将会在未来停止使用。

感谢@incNick 回答,已采纳答案。
另外补充一些内容
就算只有有一个连接的情况

<code class="php">$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>";
}</code>
Copy after login

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 之类的不鼓励使用了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!