使用准备好的语句并处理php和mysql中错误的正确方法

时间:2018-08-27 05:34:01

标签: error-handling prepared-statement

这是我用来检查用户帐户是否已激活的功能。

//check if account is activated
function check_acc_activation($uid){
$connection = connect_db();  /*custom function for DB connection*/
$query = "SELECT acc_activated FROM user WHERE user_id=? AND acc_activated = 1";
$stmt = mysqli_prepare($connection,$query);
if($stmt){
    $is_activated = false;
    if(mysqli_stmt_bind_param($stmt,'s',$uid)){
        if( mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            if(mysqli_num_rows($result) > 0){
                $is_activated = true;
            }
        } else{
            exit("MessageToUser: Unexpected error! Please try later.");
        }
    }
    else{
        exit("MessageToUser: Unexpected error! Please try later.");
    }

}
else{
    exit("MessageToUser: Unexpected error! Please try later.");
}
mysqli_free_result($result);
mysqli_stmt_close($stmt);
mysqli_close($connection);
return $is_activated;
}

我的问题-使用准备好的语句处理错误是否正确?或用准备好的语句处理错误的最有效方法是什么? 我发现了一些HERE。谢谢。

0 个答案:

没有答案