0

I try throw exception when there is no result in database, here is code:

try {
    if (!$stmt->execute()) {
       throw new ErrorExeption('there is no row with that id');
    }
} catch (ErrorExeption $e) {
    echo $e->getMessage();
}

So, when I paste wron Id I cant see the error message. What I am doing wrong?

2
  • 6
    execute will only return false if there's an error. If it there are no matches it will return true, but the first attempt to fetch a row will return false.
    – Barmar
    Commented Jan 12, 2015 at 9:26
  • I think you might want to change 'execute' to 'fetch' because this is when you'll actually know if there are any rows or not.
    – H2ONOCK
    Commented Jan 12, 2015 at 9:27

2 Answers 2

3

Consider this:

try {
    $stmt->execute();

    if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        throw new ErrorExeption('there is no row with that id');
    }
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (ErrorExeption $e) {
    echo $e->getMessage();
}
0
1

Could you please consider to check the spelling on ErrorExeption to ErrorException ?

Because I tried this on my local server, and the script can catch error message that we throw above.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.