PHP PDO + exception MySQL warning a thing of the past?

We receive both PDOException and warnings. These warnings drive us crazy.

Warning: PDOStatement :: execute (): MySQL server went to /home/Database.php on line 120

Warning: PDOStatement :: execute (): Error reading the result set header in /home/Database.php on line 120

Here is the code that does this - it just simulates 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 raise a warning and an exception. The above code generates both values, even if we specifically indicate PDO to select exceptions.
The above code does this faster than waiting for our servers by default wait_timeout.

2: , . , PHP , PDO?

+2
2

wait_timeout 1, 3, ? MySql , "Mysql Server " , 3.

http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_wait_timeout

MySQL 2006: mysql

2

:

  • low wait_timeout - : ping, .
  • - : tune max_allowed_packet my.cfg

PDO - ping PDO ping MySQL db PDO

3

(afaik) - (, E_ERROR) error_reporting. pdo, , E_ERROR reset .

PDO / (sic!) . , ( setAttribute constructor), / pdo-throw :). .

+3

,

$db->setAttribute(PDO::ATTR_TIMEOUT,ini_get('max_execution_time'));

->setAttribute PDO.

, , .

( )

-2

Source: https://habr.com/ru/post/1628318/


All Articles