Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.