4

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("\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);
}

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

4
  • Error code 00000 means that the query way successfully executed
    – devpro
    Oct 3, 2016 at 11:28
  • Yes i know, but i dont know why no one line has been added
    – Bast
    Oct 3, 2016 at 11:30
  • 3
    Wrapping PDO in an object wrapper is a bit like having an ashtray on a motor bike. You have to know what you are doing before you try using it. PDO works just great using its existing object.
    – RiggsFolly
    Oct 3, 2016 at 11:35
  • This is not the problem here, select statement work great with that
    – Bast
    Oct 3, 2016 at 11:47

1 Answer 1

8

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.

7
  • I already set PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION. For my class something like that woul'be better? github.com/wildantea/php-pdo-mysql-helper-class
    – Bast
    Oct 3, 2016 at 11:53
  • If PDO doesn't throw an exception then there is no error and INSERT actually worked. Or may be you managed to do something extremely strange in your class. Why not to learn raw PDO inserts before starting with wrappers? Oct 3, 2016 at 11:57
  • As of the wrapper from the link you posted, it's even worse than yours - it's full of SQL injections. Oct 3, 2016 at 11:58
  • How it's possible with PDO and prepared statement? if you bind all parameters it's safe no?
    – Bast
    Oct 3, 2016 at 12:01
  • IF you bind, it is safe. But the guy who wrote that wrapper don't bother to bind. Besides, sometimes you just cannot bind. If you read the article I posted for you, you will learn how it's possible. Oct 3, 2016 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.