|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2015-09-25 14:55 UTC] s dot chernomorets at gmail dot com
 Description:
------------
In php 5.3 pdo-driver does not generate warnings for error "2006 MySQL server has gone away", but php 5.6.13 does even when PDO::ATTR_ERRMODE setted to PDO::ERRMODE_EXCEPTION
Test script:
---------------
# cat <<EOF >t.php
<?php
set_error_handler(function($e, $m) {
        echo "set_error_handler: $m; " ; var_dump($e);
}, E_ALL & ~E_STRICT);
$pdo = new PDO(
        'mysql:host=localhost;port=3306',
        'user',
        'password'
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$s = $pdo->query('select  CONNECTION_ID()');
echo "Connection id: ".$s->fetchColumn()."\n";
try {
        $pdo->query('select sleep(100)');
}catch(Exception $e) {
        echo "Exception: ".$e->getmessage();
        //var_dump($e);
}
EOF
# php t.php  &
Connection id: 610
# mysql -h localhost -e 'kill 610'
# We kill mysql thread with id "610" from string "Connection id: 610"
Expected result:
----------------
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Actual result:
--------------
set_error_handler: PDO::query(): MySQL server has gone away; int(2)
set_error_handler: PDO::query(): Error reading result set's header; int(2)
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
Patchesphp56-mysqlnd-warnings.pat (last revision 2015-09-25 14:56 UTC by s dot chernomorets at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 20:00:01 2025 UTC | 
I use this as a more easily repeatable test: <?php $conf = [ 'host' => 'myHostname', 'user' => 'myUsername', 'pass' => 'myPassword', 'port' => 3306, 'name' => 'myDatabaseName', ]; $dbh = new PDO("mysql:host={$conf['host']};port={$conf['port']};dbname={$conf['name']};", $conf['user'], $conf['pass']); $dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); $sql = "SELECT 1"; $sth = $dbh->prepare($sql); $sth->execute(); $sth->setFetchMode(\PDO::FETCH_NUM); $row = $sth->fetch(); echo "Got one = {$row[0]}\n"; echo "Setting wait_timeout to 3s\n"; $dbh->query("SET SESSION wait_timeout = 3"); echo "sleeping for 5s\n"; sleep(5); try { $sql = "SELECT 1"; $sth = $dbh->prepare($sql); $sth->execute(); $sth->setFetchMode(\PDO::FETCH_NUM); $row = $sth->fetch(); echo "Got one = {$row[0]}\n"; } catch (\Exception $e) { $class = gettype($e); echo "CAUGHT EXCEPTION {$class}) : ".$e->getMessage()."\n"; } echo "all done\n"; ?> Expected behavior is that it the output will contain the CAUGHT EXCEPTION line, instead it issues a PHP Warning and continues to execute. Making it impossible to catch and handle this exception: Got one = 1 Setting wait_timeout to 3s sleeping for 5s PHP Warning: PDOStatement::execute(): MySQL server has gone away in exceptionTest.php on line 30 PHP Warning: PDOStatement::execute(): Error reading result set's header in exceptionTest.php on line 30 CAUGHT EXCEPTION object) : SQLSTATE[HY000]: General error: 2006 MySQL server has gone away all done