3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vladimir
  • 1,602
  • 2
  • 18
  • 40
  • 1
    That framework doesn't use prepared statements, and `UPDATE` query will return affected rows only for MySQL. Why use it if it doesn't utilize PDO properly? It's not even tested. You could just as well use direct PDO and end up with same LoC number, plus you can handle errors properly (exceptions). – Mjh Jun 02 '15 at 15:15
  • 2
    After a closer look, that thing claims it'ts a database framework. It's not, it's a poor wrapper around PDO. You are better off using raw PDO or any of these: Eloquent, Doctrine, Propel. At least they're tested, documented and they work properly. – Mjh Jun 02 '15 at 15:19
  • @Mjh It was recommended on too many websites as a framework (when i search best php frameworks) so I thought it might be like CodeIgniter but only the database class (which was what i needed for this project and CI would be overkill), Am I handling errors correctly? – Vladimir Jun 02 '15 at 15:20
  • 1
    Apparently those were wrong websites, after a quick look I can identify multiple mistakes made. I don't know if you are handling errors correctly because that thing is inconsistent. That's why you're way better off using plain PDO or the 3 projects I mentioned. I would ditch this Medoo, it's a beginner's app at best. – Mjh Jun 02 '15 at 15:23
  • @Mjh Thank you very much, then I'm off using it, I'm looking into the 3 you mentioned, is there any one them which can be used with a simple `require_once('file.php'); $database = new db_handler();` like medoo? (Not needing to install anything one the server) – Vladimir Jun 02 '15 at 15:36
  • 1
    As far as I know, all 3 of them are composer-loadable, so you can install them via composer and just use `require_once ('vendor/autoload.php');` and that's it. – Mjh Jun 02 '15 at 15:37
  • @Mjh Thank you very much for your help. – Vladimir Jun 02 '15 at 15:39
  • I'm glad I helped, good luck! :) – Mjh Jun 03 '15 at 07:40

3 Answers3

3

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.

Shape
  • 64
  • 2
  • Thank you very much for replying, I throw away Medoo and I'm using PDO directly now (the question was for 2 weeks ago), I think that is way better than Medoo which wasted a week of my time... it is very badly written, using PDO with `try{ } catch{ }` gives me a peace of mind i never will have with Medoo... – Vladimir Jun 15 '15 at 12:25
2

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.

Pete
  • 351
  • 1
  • 6
  • 16
0

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.

Saurabh Dhar
  • 146
  • 4