使用纯 PHP 代码(而不是框架)是否有更优雅的方式来处理 mysqli 错误,如下所示:
if (!mysqli_query($con, $sql)) {
# Throw error so we can handle them
echo mysqli_error($con);
# handle a duplicate email
if (strpos(mysqli_error($con), "email_UNIQUE") !== false ) {
$_SESSION["errors_found"] = true;
array_push($_SESSION["error_messages"], "Email given is already registered.");
}
# handle a duplicate username
else if (strpos(mysqli_error($con), "username_UNIQUE") !== false ) {
$_SESSION["errors_found"] = true;
array_push($_SESSION["error_messages"], "Username is already taken.");
}
# handle any other sql query error...
else {
die("Database query error: " . mysqli_error($con));
}
}