freeloader Posted August 4, 2009 Share Posted August 4, 2009 Hi guys, I have a script that executes more or less like this: // make a sql connection if (!$cnn = mysql_connect($db_server, $db_user, $db_pwd)) { echo mysql_error(); exit(); } if (!mysql_select_db($db_db, $cnn)) { echo mysql_error(); exit(); } // do a bunch of stuff without interacting with mysql server for a few minutes // flag the account as done $sql = "UPDATE DB_Accounts SET Active = 'N' WHERE ID = '$userID'"; if (!$selecttabel = mysql_query($sql, $cnn)) { echo mysql_error(); exit(); } It outputs as follows: MySQL server has gone away It has to do with my hosting (godaddy.com) having its default mysql timeout at a few minutes, because the queries are fine and if I run the file without the few minutes of interuption, it works perfectly. I even tried to put a new mysql connect before that last query, but the result was the same, the server had timed out. Now my question is: is it possible to prevent this from happening, to auto reconnect the mysql or prevent it from timing out in the first place? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/168831-solved-mysql-disconnecting/ Share on other sites More sharing options...
fenway Posted August 4, 2009 Share Posted August 4, 2009 If there's a timeout on the server side, you have to run your script in smaller steps. Quote Link to comment https://forums.phpfreaks.com/topic/168831-solved-mysql-disconnecting/#findComment-890922 Share on other sites More sharing options...
freeloader Posted August 4, 2009 Author Share Posted August 4, 2009 There's no possibility to reconnect the mysql? Quote Link to comment https://forums.phpfreaks.com/topic/168831-solved-mysql-disconnecting/#findComment-890931 Share on other sites More sharing options...
freeloader Posted August 5, 2009 Author Share Posted August 5, 2009 Fixed it myself. function queryWithReconnect($query, $conn) { if (!$selecttabel = mysql_query($query,$conn)) { $maxcount = 5; $cnt = 1; while($cnt < $maxcount) { @mysql_close($conn); unset($conn); $conn = connect_db(); if ($selecttabel = mysql_query($query,$conn)) { return $selecttabel; } else { $cnt++; } } trigger_error(mysql_error(), E_USER_ERROR); } else { return $selecttabel; } } Based on some other stuff I found on the web. So it IS possible to reconnect as this script works for me. Quote Link to comment https://forums.phpfreaks.com/topic/168831-solved-mysql-disconnecting/#findComment-891216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.