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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵