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 management #623

Closed Philippe-M closed 3 years ago

Philippe-M commented 7 years ago

Hello, i've this code

<?php
require __DIR__.'/include/Medoo.php';
use Medoo\Medoo;

class api {
  public function __construct() {
    $this->database = new Medoo([
      'database_type' => 'mysql',
      'database_name' => 'xxxx',
      'server' => 'xxxx',
      'port' => '3306',
      'username' => 'xxx',
      'password' => 'xxxx',
      'logging' => true,
      'option' => array(
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
      )
    ]);
    }

  public function createTable($tableName) {
    switch ($tableName) {
      case 'stock_tmp':
        $this->database->query("
          CREATE TABLE `stock_tmp` (
            `itmref` varchar(20) NOT NULL,
            `status` varchar(1) NOT NULL,
            `qtystu` decimal(10,5) NOT NULL,
            `cumallqty` decimal(10,5) NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
        );
        var_dump($this->database->error());
        break;
    }
}

I call createTable a first time, I have no (normal) error. I call it a second time I have an error but it is not medoo that returns it to me but php.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'stock_tmp' already exists' in /var/www/html/plugins/portail/include/Medoo.php:306 Stack trace: #0 /var/www/html/plugins/portail/include/Medoo.php(306): PDOStatement->execute() #1 /var/www/html/plugins/portail/include/Medoo.php(276): Medoo\Medoo->exec('\n CREA...', Array) #2 /var/www/html/plugins/portail/api.php(43): Medoo\Medoo->query('\n CREA...') #3 /var/www/html/plugins/portail/test.php(15): api->createTable('stock_tmp') #4 {main} thrown in /var/www/html/plugins/portail/include/Medoo.php on line 306

php stops code execution and error is not intercepted by medoo.

How-to use medoo for intercepte error ?

ghost commented 7 years ago

You can use $database->error() for that -> https://medoo.in/api/error

Philippe-M commented 7 years ago

I use it already

  public function createTable($tableName) {
    switch ($tableName) {
      case 'stock_tmp':
        $this->database->query("
          CREATE TABLE `stock_tmp` (
            `itmref` varchar(20) NOT NULL,
            `status` varchar(1) NOT NULL,
            `qtystu` decimal(10,5) NOT NULL,
            `cumallqty` decimal(10,5) NOT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
        );
        var_dump($this->database->error());
        break;
    }

But it does not work.

ghost commented 7 years ago

i see. i never used Medoo to create temporary database so i dont know (only for the first install of the script). it may be a bug : @catfan ?

Good luck !

Philippe-M commented 7 years ago

I am trying to use error handling for any queries I make through Medoo. This example is another among many others in my scripts

catfan commented 7 years ago

If you create no problem at first time. Why do not use CREATE TABLE IF NOT EXISTS?

Philippe-M commented 7 years ago

This query is just an example. I use Medoo for all my program which makes insert, get, update ... And for the moment if a few things go wrong during the query I have a big error php and not the error handling as provided in Medoo