duandingcu7010 2016-10-03 11:25
浏览 119
已采纳

PDO未插入 - 错误代码00000

I have an issue with my INSERT query, $pdo->execute return false, with error code 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

Code:

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("", " ", $query));

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

    $rawStatement = explode(" ", preg_replace("/\s+|\t+|
+/", " ", $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);
}

Parameters array

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

I already try this query in phpmyadmin and everything worked well.

If someone know how to solve this?

thanks

PS: sorry for my bad english

  • 写回答

1条回答 默认 最新

  • dongxue163536 2016-10-03 11:48
    关注

    PDO is reported not to fill the errorInfo property in certain circumstances.

    Instead, you have to make it throw an exception, which is the most reliable way to get the error message. To do so, in your constructor, add this line

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

    Also note that your class is a genuine example of all the mistakes one could make writing a PDO wrapper. I compiled the most popular mistakes in an article, Your first database wrapper's childhood diseases and your class contains every single one of them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?