PHP PDO: Displaying errors

前端 未结 4 2041
长情又很酷
长情又很酷 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:44

    public static function qry($sql) {
        try {
        $statement = $db_handle->prepare($sql);
        $retval = $statement->execute();
        if($statement->rowCount() >= 1) { 
            //do something               
        }else {
        $errors = $statement->errorInfo();
        echo $errors[2] . ", " . $errors[1] . " ," . $errors[0];
    }
    } catch (Exception $e) {
    echo $e->getMessage();
    }        
    }
    

提交回复
热议问题