Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysqli reconnect problem

I am having trouble using the mysqli class in PHP and I haven't been able to find the answer anywhere.

In my script a class creates a mysqli connection that it uses throughout it's functions. Afterward, this script forks. The connection is used by the children as well, but I'm running into the problem of the connection being closed (MYSQL Server Has Gone Away) in the parent when the children die.

Before I switched to mysqli (was just using mysql) I simply called mysql_ping to assure that the db connection was there before performing the query in the parent process. Mysqli has a similar ping function BUT it doesn't actually reconnect if the connection is gone. I tried using the mysqli.reconnect=ON global setting with no luck (using php.ini and ini_set).

The php mysql_connect function allows you to grab an already-existing connection, so if I was using mysql instead of mysqli, I could simply reuse the connection in the child right after the process forked. BUT mysqli doesn't seem to have any such functionality...

The only thing I was able to do was call mysqli->ping() and if that returned false then reconnect to the database in the parent. This is terribly inefficient, and I would much rather figure out how to do it correctly with mysqli (and no need for manual reconnects) that having to change back to mysql..

Any suggestions?

like image 645
Evan Carothers Avatar asked Sep 23 '09 21:09

Evan Carothers


People also ask

How do I fix PHP connection error?

PHP mysqli_connect_error() function returns an string value representing the description of the error from the last connection call, incase of a failure. If the connection was successful this function returns Null.

What is CONN -> Connect_error?

Definition and Usage. The connect_error / mysqli_connect_error() function returns the error description from the last connection error, if any.

Is mysqli deprecated in PHP 7?

The oldest one uses the MySQL extension, which was deprecated as of PHP 5.5 and fully removed in PHP 7. The mysql() function no longer works in PHP 7. It has been replaced with mysqli().

What is mysqli Default_socket?

mysqli.default_socket string. The default socket name to use when connecting to a local database server if no other socket name is specified.


1 Answers

The doc for mysqli_ping() says that if you set the global option mysqli.reconnect to 1 (in your php.ini) then mysqli_ping() will reconnect when it detects the connection has gone away.

Update: Since I answered this question in 2009, PHP has mostly moved on to use the mysqlnd driver by default instead of libmysql. As mentioned in the comment below, mysqlnd does not support the mysqli_ping() function, and the PHP developers did this intentionally, according to their "Won't Fix" answer in https://bugs.php.net/bug.php?id=52561

So there appears to be no solution for automatic reconnect in PHP anymore.

like image 188
Bill Karwin Avatar answered Oct 18 '22 16:10

Bill Karwin