php - Am I handling query errors correctly in Medoo Framework? -
i'm using medoo framework handle database queries. pdo wrapper, didn't find in documentation how handle errors or check result, return empty array, false 0 etc.
as couldn't understand how handle errors i'm doing using empty() because can handle false , 0 , empty array think it's okay here):
on select (medoo returns array)
// same as: // select username accounts id=$id , suspended=0 $select = $database->select("accounts",["username"], [ "and" => [ "id" => $id, "suspended" => 0 ] ]); // have check if query failed if row not found if (empty($select) === false && count($select) > 0) { // didn't fail // username this: $key = array_keys($select)[0]; $username = $select[$key]['username']; } else { // failed } on insert (medoo says returns insert id here)
$insert = $database->insert("accounts", [ "username" => "$username" ]); // check if query didn't fail , inserted (affected rows think?) if (empty($insert) === true or $insert < 1) { // failed } on update (this clear query, returns affected rows)
$update = $database->update("accounts", ["brute_force[+]" => 1], ["id" => $user_id]); if (empty($update) === true or $update < 1) { // failed } // check if query didn't fail , affected row i confused , unsure these i'm paranoid maybe should rewrite , use codeigniter do.
to check if select/update statement succeeded use:
if(!$select){ // select failed }
because medoo return false or 0 or empty array if select/update failed or no data retrieved/updated, , of these things equal false in if statement. insert can use same thing if table in database has id field primary key; if don't have primary key, use error() method , parse response check errors, because medoo return 0 though statement executed.
Comments
Post a Comment