PHP error pconnect and connect

<?php $connp = mysql_pconnect("localhost", "root", "password"); echo mysql_stat($connp); mysql_close($connp); if(!mysql_ping($connp)){ echo " false"; } echo "<br />"; $conn = mysql_connect("localhost", "root", "password"); echo mysql_stat($conn); mysql_close($conn); if(!mysql_ping($conn)){ echo " false"; } echo "<br />"; 

? >

Hello to all,

I tested php pconnect and connected.

The above code, below is the result.

Time spent on the site: 697914 Topics: 1 Questions: 1530 Slow queries: 0 Opens: 91 Flush tables: 1 Open tables: 0 Number of queries per second: 0.2 false

Time spent on the site: 697914 Topics: 2 Questions: 1530 Slow queries: 0 Opens: 91 Hidden tables: 1 Open tables: 0 Number of queries per second: 0.2 false

As you can see, both echo false. Pconnect must not show false, right? Because pconnect is waiting for a timeout. But it's not that.

Also, when I really close $ connp, it cannot query any sql statement. Thus, pconnect does the same thing as connect.

I have already enabled persistent in php.inc. Please tell me that I am wrong. Thanks to everyone.

+4
source share
1 answer

In most cases, there is no reason to use mysql_close () in combination with persistent connections, but here is some explanation.

First of all, you must make the difference between the resource (PHP variable, internally: zval) and the connection (for example, a TCP connection or aka socket β€œlink”).

When mysql_connect () is called, a new connection is created each time, and a new resource is returned. Multiple calls to mysql_connect () in the same script will create multiple connections and the corresponding resource will be returned.

When you call mysql_pconnect (), a new connection is created only if no active persistent connection is found in the current PHP process , and the new resource is also returned every time. Multiple calls to mysql_pconnect () return different resources that all point to the same connection.

mysql_close () will have an action for both the connection and the resource :

  • If the connection indicated by the resource is not permanent, the connection is closed.
  • The resource is destroyed in each case.

This means that using mysql_close () in a persistent connection, you can no longer use the connection, even if it is still open, you can debug it with:

 <?php $connp = mysql_pconnect("localhost", "root", "password"); mysql_close($connp); sleep(60); ?> 

and looking in MySQL that the connection is still active:

 SHOW PROCESSLIST; 

If you had warnings included in your error_reporting, you would see a message like:

 Warning: mysql_ping(): 4 is not a valid MySQL-Link resource in ... on line ... 

The only way to use the previously created persistent connection is to create a new resource again that points to it:

 $connp = mysql_pconnect("localhost", "root", "password"); 

Using mysql_thread_id () in the resource will give you the MySQL connection identifier, so you can make sure mysql_close () does not close the persistent connection, but only destroys the resource:

 <?php $conn = mysql_connect("localhost", "root", "password"); echo mysql_thread_id($conn), "\n"; mysql_close($conn); $connp1 = mysql_pconnect("localhost", "root", "password"); echo mysql_thread_id($connp1), "\n"; mysql_close($connp1); $connp2 = mysql_pconnect("localhost", "root", "password"); echo mysql_thread_id($connp2), "\n"; mysql_close($connp2); ?> 

This will result in:

 61 62 62 
+2
source

Source: https://habr.com/ru/post/1337907/


All Articles