Is there a way to perform error checking with $db->query?

When using the global database manager, is there a way to catch errors? Just in case.

Ideally I would like to check for errors here before sending success.

Thanks!

  • Why doing that instead of $contact_bean->save()?

    Anyway, you can do something like that:

    $db->query($sql);

    if($db->checkError()) {

       $error = $db->lastError();

    }

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hello.

    It is a public endpoint with noLoginRequired = true due to not wanting to burden an external team with the annoyance of jugging refresh tokens or authenticating with a user and pass. The bean->save(); method does not work within  public hooks. It just crashes the hook without any logs in sugar. I could bypass this with the code below but this seems like more of a security risk than just doing a quick update with SQL, but I could be wrong.

    global $current_user;



    if (empty($current_user) || empty($current_user->id)) {

        $current_user = new User();

        $current_user->getSystemUser(); // or any other user bean

    }

    In any case, thank you for the reply.

  • You could check for the error as André Lopes has shown above although maybe $GLOBALS['db']->lastDbError() might do it too.  But, honestly, what kind of error are you expecting from that query?  At worst you could feed it an incorrect ID but it looks like you are pulling that from a bean somehow.  I am more worried about what code you are using to get $contactBean filled.

    I would add some checking to at least find that this request is coming from a form or server you control if that's possible or create some sort of approval workflow so you at least see if someone tries to use this API for something bad.

    That being said, I would go ahead and use the getSystemUser() code and convert this to a bean save.  That way you can get notifications or an Audit log or something from the event.  As long as you properly verify that $contactBean->id is a GUID and not some SQL injection you should be fine.