這是一道古老的題目
請看代碼,資料庫關閉指令將關閉哪個串連標識?( )
$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 "
";}//這個當然也能mysql_select_db('test',$link1);$rs = mysql_query('show tables;',$link1);while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "
";}
沒錯,mysql_close()誰也沒關閉。
實際上,如果把mysql_close();修改為
mysql_close($link1);或者mysql_close($link2);
下面$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 "
";}//這個當然也能mysql_select_db('test',$link1);$rs = mysql_query('show tables;',$link1);while ($row = mysql_fetch_assoc($rs)) { print_r($row); echo "
";}
沒錯,mysql_close()誰也沒關閉。
實際上,如果把mysql_close();修改為
mysql_close($link1);或者mysql_close($link2);
下面$link1,$link2仍然都是有效
如果真要close,只有這樣才好使,
mysql_close($link1);mysql_close($link2);
兩個一起close。
這是為什麼呢?為什麼呢?
樓主的測試很贊,確實如此,mysql_connect返回的肯定是一個引用,如樓主的測試說明該控制代碼不但有一個變數引用而且還有一個控制代碼引用(我自己隨便命名的),mysql_close的時候釋放控制代碼引用,然後檢查該變數的所有控制代碼引用是不是都被釋放了,如果是的話則對該變數進行記憶體回收處理。例如
$a = 'Hello';$b = &$a;$c = &$a;unset($a);echo $b . $c; //HelloHello 而不是 null
這個測試代碼說明了記憶體回收關於引用的處理,所以推測mysql_close也是類似的處理,但是與unset的管理目標是不同的(例如unset管理的是記憶體指標,mysql_close管理的是串連控制代碼指標?)
不過貌似研究這個已經完全沒有價值了,相同的事情在mysqli系列函數上不會發生:
而且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 "
";}
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 之類的不鼓勵使用了