PHP PDO异常+ MySQL警告已消失? [英] PHP PDO Exception + Warning on MySQL Has Gone Away?

查看:473
本文介绍了PHP PDO异常+ MySQL警告已消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们同时收到PDOException和警告.这些警告使我们发疯.

警告:PDOStatement :: execute():MySQL服务器已不在第120行的/home/Database.php中

警告:PDOStatement :: execute():在第120行的/home/Database.php中读取结果集的标题时出错

这是执行此操作的代码-这只是为了模拟连接断开:

$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;
}

问题是为什么这会生成警告和异常.即使我们专门告诉PDO抛出异常,上面的代码也会同时生成这两个代码.
上面的代码使它的发生比等待服务器默认的wait_timeout更快.

我不确定为什么关闭它.问题是为什么PHP会生成警告和异常,而不管PDO错误级别如何?

解决方案

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

此错误的原因:

  • 低的wait_timeout-解决方案:ping,重新连接或增加它
  • 大数据包-解决方案:在my.cfg中调整max_allowed_packet

PDO重新连接-在PDO中模拟ping 如何ping MySQL数据库并使用PDO重新连接

编辑3 问题已更新

摆脱这些警告的唯一方法(afaik)是设置期望值(例如E_ERROR)错误报告级别.例如,您可以将pdo调用包装起来以在执行之前设置E_ERROR,并在执行后重置为默认值.

PDO出于日志目的记录警告/错误(非常好!),以进行进一步的分析.您设置的属性(通过setAttribute或构造函数)仅更改pdo的错误处理/行为-是否抛出:).这两件事是分开的.

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.

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

edit

Question is duplicate of MySQL error 2006: mysql server has gone away

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

PDO reconnect - simulate ping in PDO How do i ping the MySQL db and reconnect using PDO

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.

这篇关于PHP PDO异常+ MySQL警告已消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆