php - 插入后使用 fetch 时出现 PDO 一般错误

标签 php mysql database pdo error-handling

我正在尝试使用 fetchAll() 从数据库中获取数据,除非我尝试在 insert 查询之后立即执行此操作,否则该方法有效。我不断收到的错误是“一般错误”。我在网上看到这是一个已知问题:

It seems that if you do a $statement->query() with an INSERT statement and after that a $statement->fetch() you will get an exception saying: SQLSTATE[HY000]: General error. source

有什么办法可以解决这个问题吗?

我的代码

首先,我运行插入查询:

INSERT INTO question (`qid`, `question`, `category`, `subcategory`, `explanation`, `answer_type`, `answer_options`, `answer_nav`, `answer_optional`) VALUES ('65', 'TEST', 'wisselvraag', 'wk 1 - wk 1', '<strong>Let op! Maak aantekening bij vraag.</strong>', 'yes_no', '{null,null}', '', '1')

private function get($columns = "*", $dump = false)
{
    $this->select($columns);
    $this->prepareSQL();
    if ($dump === true) {
        $this->dumpSQL();
        return [];
    }

    $this->stmt = $this->getPdo()->prepare($this->query);
    $this->stmt->execute();


    $this->incrementAmountOfQueries();
    return $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
}

执行此操作后,我立即运行 SELECT 查询:

SELECT question.* FROM question JOIN questionlist_question ON questionlist_question.`question_id` = question.`id` WHERE question.`category` = 'wisselvraag' AND questionlist_question.`questionlist_id` = '7' ORDER BY question.`qid` DESC LIMIT 1

$this->stmt->execute();
$this->stmt->fetchAll(\PDO::FETCH_ASSOC);

我的数据。 fetchAll() 似乎是问题所在。

/**
 * prepare an INSERT query
 * @param array $data
 * @return bool
 */
private function insert(Array $data)
{
    $fields = "";
    $values = "";

    foreach ($data as $field => $value) {
        $value = htmlentities($value);
        $fields .= "`$field`, ";
        $values .= "'$value', ";
    }

    $fields = trim($fields, ", ");
    $values = trim($values, ", ");
    $this->insert = "($fields) VALUES ($values)";

    $this->incrementAmountOfQueries();

    return $this->execute();
}

 /**
 * Execute a query
 * @return bool
 */
private function execute()
{
    $this->prepareSQL();
//    print($this->query);
//    exit;
    $this->stmt = $this->getPdo()->prepare($this->query);
    return $this->stmt->execute();
}

$This->stmt

$this->stmt 定义为 $this->stmt = $this->getPdo()->prepare($this->query);

getPdo()

/**
 * Get PDO instance
 * @return \PDO
 */
private function getPdo()
{
    if (!$this->pdo) {
        try {
            $this->pdo = new \PDO($this->dsn, $this->username, $this->password);
            $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        } catch (\Exception $err) {
            print_r($err);
            exit;
        }
    }

    return $this->pdo;
}

$this->查询

private function prepareSQL()
{
    $sql = "";
    if ($this->insert) {
        if ($this->table) {
            $sql .= "INSERT INTO " . $this->table;
            $sql .= " " . $this->insert;
        }
    } else {
        if ($this->select) $sql .= "SELECT " . $this->select;
        if ($this->table) $sql .= " FROM " . $this->table;
        if ($this->join) $sql .= " " . $this->join;
        if ($this->where) $sql .= " " . $this->where;
        if ($this->order) $sql .= " " . $this->order;
        if ($this->limit) $sql .= " LIMIT " . $this->limit;
    }
    $this->query = $sql;
    return $this;
}

最佳答案

是的。

我想你是从函数返回这个值。好吧,返回一个声明。如果您需要获取行,然后将 fetchAll() 链接到您的函数调用。 您不会相信您的功能将变得多么灵活。

您可以查看此类函数的示例部分:all this stuff仅通过返回的语句才可用。

您可能还想阅读我的另一篇文章,Your first database wrapper's childhood diseases因为我确信您的包装还有其他问题。

关于php - 插入后使用 fetch 时出现 PDO 一般错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41243216/

相关文章:

PHP Datepicker 数据不会进入 mySQL

php - 如何在 php 中使用 youtube api 为播放列表设置缩略图?

database - 为什么搜索索引具有对数复杂度?

database - 具有链接到同一个表的两个子表单的 MS Access 表单

c# - 在 Lucene 中索引多个表

PHP 安全性、intval 和 htmlspecialchars

php - 当从 php 执行时,mysql 导入的整理不同

php - 将 PHP 日期范围转换为 MYSQL 单个日期

php - 将枚举转换为 int 并从 MySQL 中的另一个表插入该 int 值

mysql - 选择所有并排除结果。 SQL连接