Reputation: 425
I use medoo mysql framework.
It works great and return error for all action like update/delete and...
But not for insert.
when I insert wrong data (or with wrong columns name), it returns 0 for last inserted id but also retrun 0000 for $database->error()
!
this is some of my settings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
and
'option' => [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
]
how can I force it to show errors?
I use php7.0.10 and Ampps with mysql version 5.6.31/(localhost windows10)
Upvotes: 2
Views: 743
Reputation: 350
This is due to the fact that insert() returns the last inserted ID. The medoo code looks like this:
$this->exec('INSERT INTO ' . $this->table_quote($table) . ' (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');
$lastId[] = $this->pdo->lastInsertId();
return count($lastId) > 1 ? $lastId : $lastId[0];
Now, if you would put this in between line 1 and 2:
var_dump($this->pdo->errorInfo());
You would get the error message from the INSERT command. The way medoo currently (Nov '16) handles this, you get the lastInsertId() error message, which is always 0000 (so no error).
Two solutions
For INSERTs you could use the query() method, build your own INSERT query and use error() as you usually would.
Or you could put the following in your config
'option' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
and then always use insert() inside of try-catch:
try {
$db->insert("table", ["column1" => "value1"]);
} catch (Exception $e) {
echo $e->getMessage();
}
Now you would get for example:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'value1' for key 'PRIMARY'
Upvotes: 3