PDO:使用fetchAll,仍收到一般错误:2014 [英] PDO: Using fetchAll, still receiving General error: 2014

查看:99
本文介绍了PDO:使用fetchAll,仍收到一般错误:2014的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题使我发疯...

This problem is proceeding to drive me crazy...

首先免责声明,我几乎没有正规的编程培训.我为此绊脚石.

First a disclaimer, I have very little formal programming training. I am stumbling thru this.

我收到一般错误:2014错误;首先,它是一个特定的查询(在类中建立),警告取决于我在实例化该对象的代码中的位置.我继续将每个查询更改为fetchAll,然后在fetchAll不起作用后也关闭每个查询的游标.现在有两个令人讨厌的查询.这是其中之一的副本和粘贴:

I am receiving the General error: 2014 error; at first it was one particular query (which was established in a class) and the warning was dependent upon where in the code I was instantiating the object. I proceeded to change every query to a fetchAll and then closed the cursor for every query as well after the fetchAll didn't work. Now there are two offending queries. Here is a copy and paste of one:

(更新的代码):

$sql = "select initial_state from source_nodes where id = :id";

$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->bindParam(':id', $allSources->id[$id], PDO::PARAM_INT);

if ($stmt->execute()) {
    $row = $stmt->fetchAll();
    $stmt->closeCursor();
    foreach($row as $i=>$value){
        $allSources->state[$id] = $row[$i]['initial_state'];
    }
}

不确定是否重要,但是在'if'上引发警告.据我所知,每个提取"现在都是"fetchAll",并且包括"closeCursor".

Not certain if it matters, but the warning is thrown on the 'if'. As far as I am aware, every 'fetch' is now a 'fetchAll' and includes a 'closeCursor'.

连接设置如下:

$this->dbh = new PDO($dsn, $user, $password, array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =>true
            ));

建议?

推荐答案

$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);

是解决错误的灵丹妙药,您甚至不愿提供该文本,假定读者可以凭心识数地知道每个错误.

is the silver bullet to fix the error, which text you didn't even bothered to provide, supposing readers to know every error by numbers by heart.

不是仅仅机械地替换对fetchAll()的所有fetch()调用.

While just mechanically replacing all the fetch() calls to fetchAll() is not.

已编辑代码中有两件事.

Two things on your edited code.

  1. 您的循环非常无用.正如我上面所说,机械地,无意识地做某事不会带来任何好处.一个人必须至少具有最低限度的理解.您拥有的循环是无用的两次:因为您从fetchAll()获得的$ row不需要任何循环(它已经包含所有数据),而且因为无论如何您只得到一行,因此不需要循环.因此,不应将fetch()用于fetchAll.
  2. 尽管您有这种感觉,但错误消息清楚地告诉了我们.并非现在每个'fetch'都变成了'fetchAll'".
  3. 似乎您正在一个较大的循环中逐行选择行.而且显然有一些错误查询的味道.您必须使用fetchAll()且不使用所有这些内部查询,使此外部查询(对我们不可见)一次获取所有数据.

这篇关于PDO:使用fetchAll,仍收到一般错误:2014的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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