Jump to content

[SOLVED] Mysql disconnecting


freeloader

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/168831-solved-mysql-disconnecting/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.