-1

I know this question has been there many times, but no answer works for me.

My PDO just doesn't throw any error... or something catch the error before me.

I'm quite new to php, and I hate to not understand simple things.

Here's my initialization code

$pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'], $db['user'], $db['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

and an example where I'm, for example, inserting a duplicate row with an unicity constraint (but same problem for unknown row, or any pdo exception...)

    $columnString = "....";
    $valueString = "....";
    $sql = "INSERT INTO " . $table . " (" . $columnString . ") VALUES (" . $valueString . ")";
    try {
        $stmt = $this->pdo->prepare($sql);          
        foreach($request_data as $key => $value){
            $stmt->bindValue(':' . $key,$request_data[$key]);
        }
        $stmt->execute();
    } catch (PDOException $e) {
        return null;
    }

Of course, this code works well when no database problem occurs.

I'm using Slim 3 framework, and php7. Same problem with php5.

Does it have something to do with the framework? Is there other configurations ? Why the hell exceptions are not caugh?

Martin
  • 11,881
  • 6
  • 64
  • 110
  • I think it should be: `catch(\PDOException $e){...}` (if you haven't imported and alias it) – ka_lin May 22 '18 at 14:45
  • Oh! It was this. Is it because my file was namespaced? – Martin May 22 '18 at 14:57
  • 1
    Could it be the fact that you are not doing anything when you catch an error other than returning a null? You aren't displaying the error at all. – Dave May 22 '18 at 15:26
  • Oh, this was for the example. Of course, I'm handling the error correctly. – Martin May 23 '18 at 07:47

1 Answers1

2

First, I do not see any use clause in your code snippet when you create PDO class instance or when you catch exception PDOException. If you miss them, it may give you error as PHP can not find where they are. You need to make sure you add

use PDO;
use PDOException;

in code that refer those classes.

Second, you suppress any exception related to PDO operation by using this code.

try {
    //your code
} (PDOException $e) {
    return null;
}

To be able to identify the cause of any database-related error, you need to handle exception properly. You need to replace return null; to do more meaningful action to handle the error.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • The second point was for the sake of simplicity, the first point is the answer to my question. In the comments section, ka_lin suggest to use `\PDOException` is the declaration, and you suggest `use PDOException` in the headers. Both works. What is the best practice? – Martin May 23 '18 at 07:52
  • `\PDOException` is used to refer class name using absolute namespace, while `PDOException` without `use PDOException` will cause PHP to look PDOException in current namespace. – Zamrony P. Juhara May 23 '18 at 07:57
  • ... and `use PDOException` is for _adding_ the `PDOException` in the current namespace? Hum, ok, interesting. – Martin May 23 '18 at 08:36
  • 1
    I believe `use` does not add it to current namespace, it just inform PHP where to look for. Which is global namespace. – Zamrony P. Juhara May 23 '18 at 08:57