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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器