catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Error Checking Clarification #151

Closed feelsickened closed 8 years ago

feelsickened commented 10 years ago

Hi, Medoo is great. Fantastic even! I'm having an issue with the database->error() call. Does this error data reset for each and every database query, or only when there is an error. so for example if there is an error, following by many queries without error is database->error() is set with detail from the initial error?

I have an error checking routine, but despite reporting errors, entries are still being inserting into my database. The following is inside a foreach routine. The SQL database has unique keys to avoid duplication. I am receiving duplication error msgs, but I am getting unique entries into the database as I desire, but my code below reports only duplication errors.

unset($error_checking); $error_checking = $database->error(); var_dump($error_checking); echo '
...'; $error_reason = $error_checking[2]; if (empty($error_checking[2])) { echo 'There were no errors...' . $error_reason; } else { echo 'There was an error: ' . $error_reason; }

Any assistance, greatly appreciated.

feelsickened commented 10 years ago

*bump...

saotand commented 9 years ago

medoo::error() only send data (until now) only when there is a error...

stephenharris commented 9 years ago

@feelsickened I've encountered this same problem. Any advice you can give on how to work around this?

feelsickened commented 9 years ago

I am relying on database->count to check afterwards that the appropriate rows/columns were updated or inserted. Which solves all my problems (except my wife). The error checking routine is practically useless (I have not checked the latest version of medoo for changes/updates though).

catfan commented 9 years ago

Well, I think just returning a value is not a convenient way for this.

Have to think a better way to check.

stephenharris commented 9 years ago

In case it helps anyone, I found using PDO's exception throwing to be be the simplest solution. medoo allows you to pass options to the underlying PDO instance in its constructor, so you can enable exceptions as follows:

$medoo = new \medoo(array(
    'database_type' => 'mysql',
    'server'        => $settings['mysql-host'],
    'username'      => $settings['mysql-username'],
    'password'      => $settings['mysql-password'],
    'charset'       => $settings['mysql-charset'],
    'option'        => array(
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
    )
));

Then wrap all your database queries in try/catch blocks to handle any errors.

imageslr commented 7 years ago

stephenharris actually helps me!