Justtechjobs.com Find a programming school near you






Online Campus Both


php-db | 2001011

[PHP-DB] Problems with mysql_connect and mysql_pconnect From: BrunoM (php <email protected>)
Date: 01/09/01

I encounter two kinds of problems while using MySQL (3.23.28) connections from PHP (4.0.4):
 1) with mysql_connect's, I am not able to open different connections to the same host with the same login without getting the same 'Ressouorce ID #i'. For example, running:
 -$id1=mysql_connect($thehost, $thelogin, $thepass);
 -$id2=mysql_connect($thehost, $thelogin, $thepass);
 returns $id1=1 and $id2=1, too. I really need to have distinct connections because of the connection class I use which associates one connction on each instantiated object. Why is it that it seems impossible to have two simultaneous connections to MySQL, therefore selecting a database on $id1 selects it on $id2, obviously?
 2) I have to use mysql_pconnect's to ensure that all different database servers are always available and connected in PHP, but it doesn't seem to work:
 -$id1=mysql_pconnect($thehost, $thelogin, $thepass);
 -$id2=mysql_pconnect($thehost, $thelogin, $thepass);
 with theses two connections, two different IDs are returned and, having tested efficiency of both solutions, I can say that it takes longer to run 1000 connect's thant 1000 pconnect's. then tests was:
 -1 x connect, 1x pconnect
 -timer1 starts
 -100 x connect
 -timer1 stops, timer2 starts
 -100 x pconnect
 -timer2 stops
With his method, both connections should have been established and then the bunch of connect's should last 100x the time of one connect, and the 100x pconnect should be really fast as the db is already connected. No, for me it seems like times are about
Below is the code I use to show the difference between the two methods. It seems like the mysql_pconnect causes only one connextion to the MySQL server ('mysqladmin processlist' shows a process not dying even when then PHP script has ended), but each 'pconnect' takes really longer than a 'connect', whereas 'pconnect' should do really faster after the first connection.
-------------------------------------------------------------------
$ta=new c_Timer();
$tb=new c_Timer();

$ta->start();
for ($i=0; $i<$NB; $i++)
 {
 $link = mysql_connect ($host, "test");
 echo "[".(int)$link."]";
 // mysql_close($link);
 }
$xa=$ta->stop_ms();

echo "-";

$tb->start();
for ($i=0; $i<$NB; $i++)
 {
 $link=mysql_pconnect ($host, "test");
 echo "[".(int)$link."]";
 }
$xb=$tb->stop_ms();

echo "<li>nb=$NB";
echo "<li>ta=$xa ms";
echo "<li>tb=$xb ms";
-------------------------------------------------------------------
 If you have any suggestion or idea about this problem, you would be very nice to help me, my email is: php <email protected>
 Many thanks!