Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ulbador

macrumors 68000
Original poster
Feb 11, 2010
1,554
0
So at one point in my code, I use mysqli_ping() to check if a connection to mysql is still alive. In C, doing this will actually reconnect the connection if it dies, but with PHP it just returns true or false, which is fine.

My problem is thus. On occasion, I get this error:
Code:
Message: mysqli_ping() [<a href='function.mysqli-ping'>function.mysqli-ping</a>]: Couldn't fetch mysqli

In an effort to prevent this, I have tried EVERYTHING I could think of to prevent the ping from being executed, to absolutely no avail:

Code:
if (is_object($conn) && mysqil_ping($conn))

if ($conn != null && mysqli_ping($conn))

if (!empty($conn) && mysqli_ping($conn))

if (isset($conn) && mysqli_ping($conn))

if (!is_null($conn) && mysqli_ping($conn))

try {
mysqli_ping($conn);
} catch (Exception $e){
}

I am totally at the end of my rope on this. Anyone have any suggestions?

Thanks.
 
Last edited:
Does this script run for an unusually long time or something? I believe the default timeout for MySQL is around 8 hours.

Your code below is calling mysql_ping, but your error message is for mysqli_ping, make sure you are using the right type of connection.

mysql_ping will not do automatic reconnection, however mysqli_ping will if you set the mysqli.reconnect option in php.ini or your code.
 
Does this script run for an unusually long time or something? I believe the default timeout for MySQL is around 8 hours.

Your code below is calling mysql_ping, but your error message is for mysqli_ping, make sure you are using the right type of connection.

mysql_ping will not do automatic reconnection, however mysqli_ping will if you set the mysqli.reconnect option in php.ini or your code.


Sorry, I just typed up the test cases I had used from memory quickly and copied and pasted (poorly). It is actually using mysqli_ping in the real tests. Thanks for pointing it out. I corrected this in my original post

8 hours is far too long of a timeout for our busy website. We are running a 30 second timeout right on the database out of necessity. Otherwise the resources sit there and are never released and aren't reallocated and used quickly enough.

I know mysqli_ping won't auto-reconnect (at least in some versions of PHP no matter what option you set), I just use it to test if the connection is still open. If not, I grab a new connection. Even if the ping worked and autoreconnected, the code would be fine. I just can't figure out why sometimes it tosses that error, or how to test for it beforehand.
 
Last edited:
Only three things come to mind:

1) That you are inadvertently calling mysqli_close somewhere before a query or ping, maybe in one of your includes.

2) You are overwriting $conn somewhere else.

3) Some PHP bug in an older version of PHP perhaps. Are you guys running on CentOS / RHEL? If so I would suggest upgrading to 5.2 from the testing repos and see if that helps.
 
Only three things come to mind:

1) That you are inadvertently calling mysqli_close somewhere before a query or ping, maybe in one of your includes.

2) You are overwriting $conn somewhere else.

3) Some PHP bug in an older version of PHP perhaps. Are you guys running on CentOS / RHEL? If so I would suggest upgrading to 5.2 from the testing repos and see if that helps.

I could see the close or even possibly an overwrite happening. At the same time, that is kinda what I want to be able to detect with one of the cases mentioned above.

It's a compiled-from-source version of PHP 5.3.5.
 
I could see the close or even possibly an overwrite happening. At the same time, that is kinda what I want to be able to detect with one of the cases mentioned above.

It's a compiled-from-source version of PHP 5.3.5.

I could be wrong, but I think the function is only intended to be used to determine if the server itself has closed the connection for a timeout.

I don't think it works for connections which you have explicitly closed yourself, as that connection effectively doesn't exist anymore.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.