mysql_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();修改为

mysql_close($link1);
或者
mysql_close($link2);
下面$link1,$link2仍然都是有效的

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

mysql_close($link1);
mysql_close($link2);
两个一起close。

 

0人点赞

 

 

 



作者:Mark_YU
链接:https://www.jianshu.com/p/94472f89789d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值