PHP PDO: Displaying errors

前端 未结 4 2042
长情又很酷
长情又很酷 2021-01-21 11:49

Do I need to use the try and catch block to display errors with PHP\'s PDO extension?

For example, with mysql you can usally do something like:

if ( ! $q         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-21 12:48

    You can choose what PDO does with errors via PDO::ATTR_ERRMODE:

    $pdo->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); // Raise E_WARNING.
    $pdo->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); // Sets error codes.
    // or
    $pdo->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // throws exception
    

提交回复
热议问题