views:

786

answers:

4

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?

A: 

Use http://www.php.net/manual/en/mysqli.real-connect.php ... reinstall php and check your php.ini settings

bader
A: 

u can try something a bit different .. instead of ping .. try to send a really simple low intensity query like "select now()" and see if you get any better results.

Sabeen Malik
the reason that might help ... mysql sees u as an active user instead of someone who is just holding a connection and hogging the resources .. i believe there are some timeout settings in my.ini/my.cnf which effect that behavior .. if you have have access to that u can also look into it.
Sabeen Malik
A: 

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.

Bill Karwin
That worked. I didn't realize that you couldn't use ini_set to modify this variable, and being the genius I am, I added the line to php.ini before, not knowing that there was already a mysli.reconnect=Off line further down the file. But removing that one and just using mysqli.reconnect worked and made mysqli->ping() perform just like mysql_ping. Thanks for the help!
Evan Carothers
A: 

Can't you use persistent connections? Also, is that really necessary to fork() ?

FractalizeR