doumi9618 2013-08-05 11:33
浏览 24

PDO提取将停止工作,直到创建新连接

I've created a basic class called Content for fetching content from a MySQL database, with a constructor like so:

$this->handle = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

and some methods utilizing this handle, chiefly one that fetches data and puts it into an array:

function select($query) {
    $sth = $this->handle->prepare($query);
    $sth->execute();
    $result = $sth->fetchAll();
    $sth->closeCursor();

    return $result;
}

Nothing fancy, really. And it works terrific. Except, after a dozen or so various queries, fetchAll all of a sudden returns an empty result. When I run the same query directly on the database, I do get a result. Also, when I run the query before running the other ones, I get a result as well. Even more oddly, the fetchAll at this point in my script used to work fine, but when the queries that preceded it got more numerous, it started returning empty results.

Based on these findings, I assumed the scripting and database where just fine, and it most likely had something to do with PDO. Eventually, I 'fixed' it by creating a second object from my Content class in order to fetch the data I was missing:

$contentTwo = new Content();

Of course, this should not be necessary! I'm looking for a real solution, and also an understanding as to what's going on. I've turned on all sorts of error reporting (PHP, PDO, MySQL) but there are no errors. Could it be some internal PDO mixup? Is there a maximum amount of prepared statements? Any ideas/solutions would be appreciated.

  • 写回答

1条回答 默认 最新

  • douque9982 2013-08-05 12:07
    关注

    Could it be some internal PDO mixup?

    No.

    Is there a maximum amount of prepared statements?

    No.

    Any ideas/solutions would be appreciated.

    Yes.

    The problem you faced with is not something unusual.
    Every developer has to deal with such a case once or twice a day. This is quite normal for our profession, when something goes wrong. A program is so complex a facility that sometimes it goes wrong. It is a duty of a programmer to find out and to fix the error.

    So, you have to investigate the cause.
    For such a purpose create a comprehensive logging system, and log everything you can hold of in case of an empty result returned. State of PDO object, input data, all involved variables, etc.

    This way you most likely will be able to spot the problem

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊