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();修改为
<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>
而且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 之类的不鼓励使用了

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

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
