This question already has an answer here:
- MySQL error 2006: mysql server has gone away 17 answers
We are getting both a PDOException and warnings. These warnings are driving us crazy.
Warning: PDOStatement::execute(): MySQL server has gone away in /home/Database.php on line 120
Warning: PDOStatement::execute(): Error reading result set's header in /home/Database.php on line 120
Here is the code that does this -- this is just to simulate a connection going away:
$db = new PDO('mysql:dbname=' . $name . ';host=' . $host, $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$statement = $db->prepare('SET SESSION wait_timeout = 1');
$statement->execute();
sleep(3);
try {
$statement = $db->prepare('SELECT 1');
$statement->execute();
} catch (PDOException $e) {
echo 'Exception! Err #:' . $e->errorInfo[1] . PHP_EOL;
}
EDIT: The question is why does this generate a warning and an exception. The code above just generates both even though we specifically tell PDO to throw exceptions.
The code above makes it happen faster than waiting for our servers default wait_timeout.
EDIT 2: I'm not sure why this was closed. The question is WHY is PHP spawning both a Warning, and an Exception regardless of the PDO Error Level?