1

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?

PersianMan
  • 924
  • 1
  • 12
  • 29

1 Answers1

0

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;
}

https://github.com/catfan/Medoo/blob/6798b9698ee60ee98a4d342485904322922b2b3b/src/Medoo.php#L1469-L1472

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:

  1. When using developer mode
  2. Or if PDO->prepare returns false/null (Not very reliable)

I personally dont like Medoo because of its bad error handling though. I like to use other tools.

Andrew
  • 300
  • 2
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/17361605) – FluffyKitten Sep 18 '17 at 02:25
  • Your answer still does not explain *why* you think the second option is safer. – FluffyKitten Sep 18 '17 at 02:36
  • Edited post above again. – Andrew Sep 18 '17 at 02:44