PDO未插入 - 错误代码00000

时间:2016-10-03 11:25:38

标签: php mysql pdo

我的INSERT查询存在问题,$ pdo->执行返回false,错误代码为00000

Query
string 'INSERT INTO module_test (img_name, description, priority) VALUES(:img_name, :description, :priority)' (length=100)

errorInfo() return:
array (size=3)
  0 => string '00000' (length=5)
  1 => null
  2 => null

代码:

private function Init($query, $parameters = "")
{
    # Connect to database
    if (!$this->bConnected) {
        $this->Connect();
    }
    try {
        # Prepare query
        $this->sQuery = $this->pdo->prepare($query);

        # Add parameters to the parameter array 
        $this->bindMore($parameters);

        # Bind parameters
        if (!empty($this->parameters)) {
            foreach ($this->parameters as $param => $value) {

                $type = PDO::PARAM_STR;
                switch ($value[1]) {
                    case is_int($value[1]):
                        $type = PDO::PARAM_INT;
                        break;
                    case is_bool($value[1]):
                        $type = PDO::PARAM_BOOL;
                        break;
                    case is_null($value[1]):
                        $type = PDO::PARAM_NULL;
                        break;
                }
                // Add type when binding the values to the column
                $this->sQuery->bindValue($value[0], $value[1], $type);
            }
        }
        # Execute SQL 
        var_dump($query);
        var_dump($this->sQuery->execute());
        var_dump($this->sQuery->errorInfo());
    }
    catch (PDOException $e) {
        # Write into log and display Exception
        echo $this->ExceptionLog($e->getMessage(), $query);
        die();
    }

    # Reset the parameters
    $this->parameters = array();
}
public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
    $query = trim(str_replace("\r", " ", $query));

    $this->Init($query, $params);

    $rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $query));

    # Which SQL statement is used 
    $statement = strtolower($rawStatement[0]);
    if ($statement === 'select' || $statement === 'show') {
        return $this->sQuery->fetchAll($fetchmode);
    } elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
        return $this->sQuery->rowCount();
    } else {
        return NULL;
    }
}
public function insert($table, $keyValue)
{
    $fieldString = '';
    $valueString = '';
    $i           = 1;
    foreach ($keyValue as $key => $currKeyValue) 
    {
        $fieldString .= $key;
        $valueString .= ':'.$key;
        if($i != count($keyValue))
        {
            $fieldString .= ', ';
            $valueString .= ', ';
        }
        $i++;
    }
    $query       = 'INSERT INTO '.$table.' ('.$fieldString.') VALUES('.$valueString.')';
    $this->query($query, $keyValue);
}

参数数组

F:\Dev\wamp\wamp64\www\include\class\Database.class.php:216:
array (size=3)
  'img_name' => string 'ttt1' (length=4)
  'description' => string 'ttt1' (length=4)
  'priority' => int 0

我已经在phpmyadmin中尝试了这个查询,一切运行良好。

如果有人知道如何解决这个问题?

感谢

PS:抱歉我的英文不好

1 个答案:

答案 0 :(得分:5)

据报道PDO在某些情况下不会填写errorInfo财产。

相反,您必须使其抛出异常,这是获取错误消息的最可靠方法。为此,请在构造函数中添加此行

$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

另请注意,您的类是编写PDO包装器时可能出现的所有错误的真实示例。我在一篇文章Your first database wrapper's childhood diseases中汇编了最受欢迎的错误,而您的班级包含了每一篇错误。