I have been scratching my head trying to figure out what is causing an intermittent error in my script. The error is: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away.
My script below is part of a function that does a curl, gets some values from a JSON response, and then writes them to a table. I'd say 80% of the time it works fine and then the other 20% i get the server gone away error. I haven't been able to identify any trends that causes it to error out, it just seems to be random. Any ideas why i might be getting this error? thanks for checking this out
...
//post via cURL
$ch = curl_init( $url );
$timeout = 500;
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$this->response = curl_exec( $ch );
$this->json_decoded = json_decode($this->response);
$this->full = print_r($this->json_decoded, true);
$client_leadid = $this->json_decoded->Parameters->lead_id;
$client_status = $this->json_decoded->Status;
$length = curl_getinfo($ch);
curl_close($ch);
//record in DB
$insert = $this->full.' | '.$url.' | '.$myvars.' | '.$this->date . ' | Time Taken: '.$length['total_time'];
$db->exec("UPDATE table SET client_resp = '$insert' WHERE global_id = '$this->leadid' LIMIT 1");
$db->exec("UPDATE table SET client_leadid = '$client_leadid' WHERE global_id = '$this->leadid' LIMIT 1");