0

我有一个安装方法,用于我的 cms 女巫读取 sql 文件并执行它们。到目前为止一切顺利..但我还需要对数据库进行一些初始化(插入一些动态值)

    $q = "INSERT INTO `tbl_users_roles` (`userId`, `roleId`) VALUES
            (1,3),
            (2,4);";
    $db->query($q, Adapter::QUERY_MODE_EXECUTE);

    $q = "INSERT INTO `tbl_users` (`username`, `password`, `accountStatus`) VALUES
            ('serverAdmin',?,1),
            ('admin',?,1);";
    $db->query($q, array($passAdmin, $passUser));

这些插入被执行,但下一个 sql 文件抛出异常:

PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

ZF2 PDO 语句没有 fetchAll 或 closeCursor。那么如何解决这个问题?

4

2 回答 2

4

好的,我将最后一个查询更改为:

    $st = $db->query($q);
    $st->execute(array(serialize($perm)))->getResource()->closeCursor();

这可行,但其他数据库驱动程序也有closeCursor吗?应该有一个通用的方法来做到这一点:(

于 2013-11-28T10:12:33.283 回答
0

我以这种方式解决了:

$sql = "
    SET FOREIGN_KEY_CHECKS = 0;
    TRUNCATE TABLE `customer`;
    TRUNCATE TABLE `customer_address`;
    TRUNCATE TABLE `customer_contact`;
    SET FOREIGN_KEY_CHECKS = 1;
    ";

$result = $this->adapter->query($sql)->execute();
$result->getResource()->closeCursor();
于 2016-12-10T11:53:56.870 回答