When PHP PDOSstatement::execute fails, will it return False or throw Exception?

Asked 1 years ago, Updated 1 years ago, 111 views

When PDO::ERRMODE_EXCEPTION,

try{
    $stmt = $pdo->prepare("SELECT* FROM po");
    $result=$stmt->execute();
    if(!$result){
        through new Exception("execute_false");
    }
} catch(Exception$e){
    var_dump($e->getMessage());
}


Base table or view not found: 1146 Table'hoge_table.po'doesn't exist
I feel that execute() is throwing an exception instead of false.

However, when you look at the document, there is no mention that execute makes an exception.For example, prepare says to make an exception.

In this regard, execute did not make an exception, but prepare did not make an exception. Is it correct to understand that execution of execute causes execute to appear to have made an exception?

Also, what is the failure of execute itself to return false?

Thank you for your cooperation.

php pdo

2022-09-29 22:28

1 Answers

PDO::ERRMODE_EXCEPTION is configured to execute(); errors in also result in PDOException.

$stmt->execute(); returns false when SQL grammar is incorrect and
When ? is specified but no parameters are specified to pass.

false example, incorrect grammar

$stmt=$pdo->prepare("wSELECT* FROM test where test=?");
$stmt->execute(array('test1'));

No parameters specified to pass

$stmt=$pdo->prepare("SELECT* FROM test where test=?");
$stmt->execute();


2022-09-29 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.