用純PHP代碼(而不是框架)是否有處理mysqli的錯誤更優雅的方式,如下圖所示: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));
}
}
在這種情況下是的。你有更好的方法來建議嗎?非常感謝您的幫助。 – 2013-02-11 01:50:58