Re: A strange question about mysql_close()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/12/17 唐家兴 <tjxing.1988@xxxxxxxxx>:
> I want to ask a strange question.
>
> As we know, if we connect a mysql database in the same host twice with the
> same username, we won't get two new link, but two handle to only one link.
> Then, if I close one of them, whether the other one will be closed together?
>
> I wrote two pieces of code. The first is like this
> <?php
> $db1 = mysql_connect($host, $username, $pwd);
> $db2 = mysql_connect($host, $username, $pwd);
> $sql = 'show databases';
> mysql_close($db1);
> $res = mysql_query($sql, $db2);
> $a = mysql_fetch_row($res);
> print_r($a);
> ?>
> Then the output of the $db2 is correct. So the $db2 isn't closed together
> with the $db1.
>
> But when I add a line among the code, the result is different
> <?php
> $db1 = mysql_connect($host, $username, $pwd);
> $db2 = mysql_connect($host, $username, $pwd);
> $sql = 'show databases';
> mysql_close($db1);
> $db1 = true; //a new line
> $res = mysql_query($sql, $db2);
> $a = mysql_fetch_row($res);
> print_r($a);
> ?>
> Then the output is wrong. The PHP tell me that the link is not a valid one.
> I don't know how an assignment operation for the $db1 can close the $db2.
> Could someone help me.
> I'll appreciate your help.
>

When you xxx_connect() with the same parameters, internally, you are
sharing the connection. It seems that killing one of the references
kills all the references.

The solution is to a use the fourth parameter on mysql_connect(). The
"new_link" param.

So ...

<?php
$db1 = mysql_connect($host, $username, $pwd, True); // Make sure a new
link is created.
$db2 = mysql_connect($host, $username, $pwd, True); // Make sure a new
link is created.
$sql = 'show databases';
mysql_close($db1);
$db1 = true; //a new line
$res = mysql_query($sql, $db2);
$a = mysql_fetch_row($res);
print_r($a);
?>

should work.

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux