Am I handling query errors correctly in Medoo Framework?

4.9k Views Asked by At

I'm using Medoo Framework to handle my database queries. It is basically a PDO wrapper, I didn't find in their documentation how to handle errors or check the result, sometimes it return empty array, sometimes FALSE sometimes 0 etc.

As I couldn't understand how to handle errors this is what I'm doing currently using empty() because it can handle FALSE , 0 and empty Array I think it's okay here):

On SELECT (Medoo returns array)

// Same as:
// SELECT username FROM accounts WHERE id=$id AND suspended=0

$select = $database->select("accounts",["username"], [
    "AND" => [
        "id" => $id,
        "suspended"   => 0
    ]
]);

// I have to check if Query failed also if row was not found

if (empty($select) === FALSE && count($select) > 0)
{
      // It didn't FAIL
      // So i get username like this:
      $key      = array_keys($select)[0];
      $username = $select[$key]['username'];
}
else
{
      // It FAILED
}

On INSERT (Medoo says it returns INSERT ID here)

$insert = $database->insert("accounts", [
    "username"        => "$username"
]);

// Check if query didn't fail and actually inserted (affected rows i think?)

if (empty($insert) === TRUE OR $insert < 1)
{
    // It Failed
}

On UPDATE (This is actually the only clear query, it returns affected rows)

$update = $database->update("accounts", ["brute_force[+]" => 1], ["id" => $user_id]);

if (empty($update) === TRUE OR $update < 1)
{
     // It FAILED
}
// Check if query didn't fail and also affected row

I am so confused and unsure about these that I'm paranoid maybe I should just completely rewrite and use CodeIgniter like I always do.

3

There are 3 best solutions below

1
On BEST ANSWER

To check if SELECT/UPDATE statement succeeded I use:

if(!$select){ // SELECT failed }

because MEDOO will return FALSE or 0 or empty Array if SELECT/UPDATE failed or no data were retrieved/updated, and all of these things are equal FALSE in an if statement. For INSERT you can use the same thing if your table in database has ID field as primary key; if you don't have primary key, then you could use error() method and parse response to check for errors, because MEDOO will return 0 even though the statement was executed.

0
On

The return object of update() is PDOStatement, so you can use its methods to get more information.

$data = $database->update("account", [
    "age[+]" => 1
], [
    "user_id[>]" => 100
]);

Returns the number of rows affected by the last SQL statement

echo $data->rowCount();

You can check if its greater then Zero or is_numeric() function.

0
On

I almost didn't look at Medoo as a result of the earlier comments on this post but I'm glad I did so would like to correct some of the statements made in earlier replies to this question so potential MeDoo users are not dissuaded from using it.

Disclaimer - all my work with Medoo has been with mySQL databases.

It is a wrapper around PDO but so are the other tools mentioned so I'm not sure of the purpose of that comment. The other tools mentioned provide more functionality in terms of ORM. As a consequence of that, they base all their endpoints/methods on individual tables in the database whereas Medoo is based on the traditional CRUD methods which, in my opinion, makes more sense. If you don't care about ORM, Medoo is fine.

You can check for any select/get/insert/update/delete errors by looking at key 2 of the error() method. If it is NULL, no error occurred; if an error occurred it will contain a string error message.

Medoo does use prepared statements, maybe it didn't back in 2015 but it does now.

I'm not sure what the basis is for the statement "it isn't tested". I haven't come across any bugs so far.

Bottom line: if you are looking for a simple query builder that uses PDO and don't care about ORM, Medoo will probably work well for you.