best way to handling error in medoo insert query or other query
The code in all queries properly. For example, when multiple records insert whid insert() method output is an array.
1:
try{
$db->pdo->beginTransaction();
$deletes=$db->delete("table",array("c_id"=>$id));
if($deletes===false || $deletes===null){
throw new Exception();
}
$res=$db->insert("tm_channel_admins",$insertsArray);
if($res===false || $res===null){
throw new Exception();
}
$db->pdo->commit();
}catch (Exception $e){
$db->pdo->rollBack();
exit("Error");
}
2:
$res=$db->insert("tm_channel_admins",$insertsArray);
if($db->error()[0]!==0){
throw new Exception();
}
what is true and safe?
The second one is more safe. The first one will still work but would be a bit more ambiguous as it returns the PDO statement object. The second one is more reliable since it uses PDO's error handling function.
public function error()
{
return $this->statement ? $this->statement->errorInfo() : null;
}
http://php.net/manual/en/pdostatement.errorinfo.php
As of the current version of medoo right now (v1.4.5), the query will only return false:
I personally dont like Medoo because of its bad error handling though. I like to use other tools.