PDO MySQL->“ SQLSTATE [HY000]:常规错误:2014当其他未缓冲的查询处于活动状态时,无法执行查询。”根据简单的插入请求

时间:2019-03-08 17:45:32

标签: php mysql pdo

当我的代码似乎与错误的主要原因无关时,我将遇到此错误。 我仅尝试使用传统方法将传统数据插入数据库。 但是对于这种情况,此错误无处不在并阻塞了我的机器。我尝试了在Internet上找到的大多数解决方案,但没有成功。这是我的代码。

<?php
namespace App\Util;

require_once 'config.php';

class Connection {
    private static $connection;

    public static function getConnection(){
        if (self::$connection == null) {
            try {
                self::$connection = new \PDO("mysql:host=".HOST.";dbname=".DBNAME.";port=".PORT.";charset=utf8", USER, PASSWORD);
            } catch (Exception $ex) {
                die('Connexion échouée '. $ex->getMessage());
            }  
        }

        return self::$connection;
    }
}
?>

<?php
namespace App\Dao;

use App\Util\Connection;

class Dao {
    protected $connection;
    protected $requete;
    function __construct() {
        $this->connection = Connection::getConnection();
        $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this->connection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, TRUE);
    }
}
?>

<?php

namespace App\Dao;

use App\Dao\Dao;
use App\Dao\DaoPeriode;

class DaoDeclaration extends Dao{

    public function addFromArray($declaration){

        $extensionId = $declaration["extension"]["extensionId"];
        $periode = $declaration["periode"];
        //$lignes = $declaration["lignes"];
        $userId = $declaration["user"]["userId"];

        $daoP = new DaoPeriode();
        $periodeDec = $daoP->getPeriode2($periode["mois"], $periode["annee"]);
        $periodeId = $periodeDec->getPeriodeId();

        $format = 'Y-m-d H:i:s';

        $time = \DateTime::createFromFormat($format, str_replace("T", " ", $declaration["dateDeclaration"]), null) ;
        $date = $time->format($format); 

        try {

            $this->requete = $this->connection->prepare("call add_declaration(:v_extension_id, :v_periode_id, :v_user_id, :v_date_declaration)");
            //$this->requete = $this->connection->prepare($sql);
            $this->requete->bindValue(':v_extension_id', (int)$extensionId, \PDO::PARAM_INT);
            $this->requete->bindValue(':v_periode_id', (int)$periodeId, \PDO::PARAM_INT);
            $this->requete->bindValue(':v_user_id', (int)$userId, \PDO::PARAM_INT);
            $this->requete->bindValue(':v_date_declaration', $date);

            $this->requete->execute();

            $data = $this->requete->fetchAll(\PDO::FETCH_ASSOC);

            $id = count($data) > 0 ? $data[0]["id"] : 0;

            $this->requete->closeCursor();

        } catch (\PDOException $e) {
            echo $e->getMessage();
        }
    }
?>

通过调用DaoDeclaration的addFromArray方法,我收到了上面的错误消息。

<?php
require_once __DIR__.'/../vendor/autoload.php';

use App\Dao\DaoDeclaration;

    $declaration = json_decode($declarationFromJson, true);
    $dao = new DaoDeclaration();
    $feed = $dao->addFromArray($declaration);

    //echo json_encode($feed > 0 ? 1 : 0);

    }

有人可以帮我吗?

0 个答案:

没有答案