'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active

前端 未结 2 640
無奈伤痛
無奈伤痛 2021-01-21 00:38

I know this question has been asked many times, but the commonly-accepted answers didn\'t help me. But quite by accident I stumbled on an answer.

Here\'s the setup: I h

相关标签:
2条回答
  • 2021-01-21 00:52

    Trigger can be created via PDO::exec() even when PDO::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);
    
    $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;");
    
    0 讨论(0)
  • 2021-01-21 01:05

    Took a bit of fiddling, but I found that when I took the ATTR_EMULATE_PREPARES=false out (the default is to emulate), it worked:

    <?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();
    ?>
    

    Hope this helps someone

    0 讨论(0)
提交回复
热议问题