php mysql 返回警告,PHP PDO异常+ MySQL警告已消失?

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?

解决方案

You set wait_timeout to 1 then you sleep 3, what will happen? MySql will close connection after one second and you will get error "Mysql Server has gone away" with next statement 'cause you sleep for 3.

edit

edit 2

Causes of this error:

low wait_timeout - solution: ping, reconnect or increase it too

large packets - solution: tune max_allowed_packet in my.cfg

edit 3 question updated

The only way (afaik) to get rid of these warnings is to set expected (eg. E_ERROR) error_reporting level. You could wrapped pdo calls up in for example to set E_ERROR before and reset to default after execution.

PDO logs warnings/errors for logs purposes (sic!) for further analytics. The attribute you set (by setAttribute or constructor) only changes error handling/behavior of pdo - throw or not:). These two things are separated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值