PDOStatement :: fetch偶尔会返回false,但是errorInfo()为空 [英] PDOStatement::fetch occasionally returns false, but errorInfo() is empty

查看:63
本文介绍了PDOStatement :: fetch偶尔会返回false,但是errorInfo()为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PDO时遇到了奇怪的问题.代码是这样的:

I am having strange problem with PDO. Code is like this:

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
  $sth = $dbh->prepare("SELECT ... FROM .... ");
  $sth->execute();
  $result = $sth->fetch(PDO::FETCH_ASSOC);
  if (!$result) {
    print_r($dbh->errorInfo());
    print_r($sth->errorInfo());
  }
} catch(PDOException $e) {
  print_r($e->getMessage());
}

因此,这是PDO的非常普遍的用法.而且该代码大多数时候都可以正常工作.但是偶尔$result是错误的.奇怪的是,PDO :: errorInfo()和PDOStatement :: errorInfo()都返回空数组,如下所示:

So it's very common use of PDO. And the code works as expected most of the time. But occasionally $result is false. What is strange is that both PDO::errorInfo() and PDOStatement::errorInfo() return empty array like this:

array(3) {
  [0]=> string(5) "00000"
  [1]=> NULL
  [2]=> NULL
}

我怀疑MySQL连接有问题,但是连接工作正常,不会引发任何异常,MySQL服务器处于空闲状态,有足够的可用连接. MySQL或PHP日志中没有错误.

I suspect there is some problem with MySQL connection, however connection works fine, doesn't throw any exceptions, MySQL server is idle, there is enough connections available. There are no errors in MySQL or PHP log.

所以我的问题是,如何进一步解决此问题?我需要知道,为什么有时fetch()失败并显示false并且在errorInfo()中没有有关错误的信息

So my question is, how to troubleshoot this problem more ? I need to know, why sometimes fetch() fails with false and there is no information about error in errorInfo()

推荐答案

好的,我发现了造成这种奇怪行为的原因.我在此MySQL服务器上有2个非常相似的数据库.我对它们两个都使用持久连接.有时(???)PHP的PDO是在选择了不同数据库的情况下创建的.

OK, I have found out what was causing this strange behaviour. I have 2 very similar databases on this MySQL server. And I use persistent connections with both of them. And somehow (???) PHP's PDO is sometimes created with different database selected.

我从没想到过这一点,因为在new PDO();中,我需要使用正确的数据库.但是由于某些奇怪的原因,连接到了不同的数据库.它必须与使用持久性连接有关,因为当我禁用持久性连接时,一切都可以正常工作.

I would never expect this, because in new PDO(); I have right database I need to use. But for some strange reason connection is made to different database. It has to do something with using persistent connections, because when I disable using persistent connection, everything works fine.

所以我可能要问另一个问题-这次-为什么在使用持久性时PDO应该连接到不同的数据库.

So I am probably going to ask another question - this time - why PDO connects to different database when it's supposed to when using persistence.

这篇关于PDOStatement :: fetch偶尔会返回false,但是errorInfo()为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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