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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable