带有消息'SQLSTATE [HY000]的'PDOException':常规错误:2014在其他未缓冲的查询处于活动状态时无法执行查询

时间:2013-06-10 19:29:08

标签: php mysql pdo

我知道这个问题已被多次询问,但普遍接受的答案对我没有帮助。但偶然的我偶然发现了答案。

这是设置:我有一大堆查询(主要是CREATE TABLE)通过连接。但是CREATE TRIGGER一直在抛出可怕的2014年错误。这与开放游标无关,因为即使它是程序中唯一的命令也是如此。例如,这失败了:

<?php
$db = new PDO ($cnstring, $user, $pwd);
$db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute (PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute (PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$st = $db->query ("CREATE TRIGGER `CirclesClosureSync` AFTER INSERT ON Circles
FOR EACH ROW BEGIN
    INSERT INTO CirclesClosure (ancestor, descendant) 
    SELECT ancestor, NEW.ID from CirclesClosure WHERE descendant=NEW.Parent;
    INSERT INTO CirclesClosure (ancestor, descendant) values (NEW.ID, NEW.ID);
END;");

$st->closeCursor();
?>

这似乎与涉及创建存储过程的其他问题类似。

这是php 5.4.5,MySql 5.5,Windows XP(虽然它在其他Windows上也失败了)

2 个答案:

答案 0 :(得分:6)

采取了一些摆弄,但我发现当我拿出ATTR_EMULATE_PREPARES = false时(默认是模拟),它起作用了:

<?php
$db = new PDO ($cnstring, $user, $pwd);
$db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$db->setAttribute (PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute (PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$st = $db->query ("CREATE TRIGGER `CirclesClosureSync` AFTER INSERT ON Circles
FOR EACH ROW BEGIN
    INSERT INTO CirclesClosure (ancestor, descendant) 
    SELECT ancestor, NEW.ID from CirclesClosure WHERE descendant=NEW.Parent;
    INSERT INTO CirclesClosure (ancestor, descendant) values (NEW.ID, NEW.ID);
END;");

$st->closeCursor();
?>

希望这有助于某人

答案 1 :(得分:2)

即使PDO :: ATTR_EMULATE_PREPARES = false,也可以通过PDO :: exec()创建触发器:

<?php
$db = new PDO ($cnstring, $user, $pwd);
$db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute (PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute (PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

$db->exec("CREATE TRIGGER `CirclesClosureSync` AFTER INSERT ON Circles
FOR EACH ROW BEGIN
    INSERT INTO CirclesClosure (ancestor, descendant) 
    SELECT ancestor, NEW.ID from CirclesClosure WHERE descendant=NEW.Parent;
    INSERT INTO CirclesClosure (ancestor, descendant) values (NEW.ID, NEW.ID);
END;");