A strange question about mysql_close()
A strange question about mysql_close()
am 17.12.2009 04:58:51 von tjxing.1988
--000e0cd519e61f3b89047ae4a4aa
Content-Type: text/plain; charset=ISO-8859-1
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
$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
$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.
--000e0cd519e61f3b89047ae4a4aa--
Re: A strange question about mysql_close()
am 17.12.2009 11:20:54 von Richard Quadling
2009/12/17 å家å
=B4 :
> I want to ask a strange question.
>
> As we know, if we connect a mysql database in the same host twice with th=
e
> 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 togeth=
er?
>
> I wrote two pieces of code. The first is like this
>
> $db1 =3D mysql_connect($host, $username, $pwd);
> $db2 =3D mysql_connect($host, $username, $pwd);
> $sql =3D 'show databases';
> mysql_close($db1);
> $res =3D mysql_query($sql, $db2);
> $a =3D 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
>
> $db1 =3D mysql_connect($host, $username, $pwd);
> $db2 =3D mysql_connect($host, $username, $pwd);
> $sql =3D 'show databases';
> mysql_close($db1);
> $db1 =3D true; //a new line
> $res =3D mysql_query($sql, $db2);
> $a =3D mysql_fetch_row($res);
> print_r($a);
> ?>
> Then the output is wrong. The PHP tell me that the link is not a valid on=
e.
> 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 ...
$db1 =3D mysql_connect($host, $username, $pwd, True); // Make sure a new
link is created.
$db2 =3D mysql_connect($host, $username, $pwd, True); // Make sure a new
link is created.
$sql =3D 'show databases';
mysql_close($db1);
$db1 =3D true; //a new line
$res =3D mysql_query($sql, $db2);
$a =3D mysql_fetch_row($res);
print_r($a);
?>
should work.
--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: A strange question about mysql_close()
am 17.12.2009 11:22:20 von Richard Quadling
2009/12/17 Richard Quadling :
> 2009/12/17 å家å
=B4 :
>> I want to ask a strange question.
>>
>> As we know, if we connect a mysql database in the same host twice with t=
he
>> same username, we won't get two new link, but two handle to only one lin=
k.
>> Then, if I close one of them, whether the other one will be closed toget=
her?
>>
>> I wrote two pieces of code. The first is like this
>>
>> $db1 =3D mysql_connect($host, $username, $pwd);
>> $db2 =3D mysql_connect($host, $username, $pwd);
>> $sql =3D 'show databases';
>> mysql_close($db1);
>> $res =3D mysql_query($sql, $db2);
>> $a =3D mysql_fetch_row($res);
>> print_r($a);
>> ?>
>> Then the output of the $db2 is correct. So the $db2 isn't closed togethe=
r
>> with the $db1.
>>
>> But when I add a line among the code, the result is different
>>
>> $db1 =3D mysql_connect($host, $username, $pwd);
>> $db2 =3D mysql_connect($host, $username, $pwd);
>> $sql =3D 'show databases';
>> mysql_close($db1);
>> $db1 =3D true; //a new line
>> $res =3D mysql_query($sql, $db2);
>> $a =3D mysql_fetch_row($res);
>> print_r($a);
>> ?>
>> Then the output is wrong. The PHP tell me that the link is not a valid o=
ne.
>> 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 ...
>
>
> $db1 =3D mysql_connect($host, $username, $pwd, True); // Make sure a new
> link is created.
> $db2 =3D mysql_connect($host, $username, $pwd, True); // Make sure a new
> link is created.
> $sql =3D 'show databases';
> mysql_close($db1);
> $db1 =3D true; //a new line
> $res =3D mysql_query($sql, $db2);
> $a =3D mysql_fetch_row($res);
> print_r($a);
> ?>
>
> should work.
>
http://docs.php.net/mysql_connect
--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php