Wednesday, 15 July 2015

PHP More elegant way to handle mysqli errors -



PHP More elegant way to handle mysqli errors -

with pure php code (and not frameworks) there more elegant way handle mysqli errors shown below:

if (!mysqli_query($con, $sql)) { # throw error can handle them echo mysqli_error($con); # handle duplicate email if (strpos(mysqli_error($con), "email_unique") !== false ) { $_session["errors_found"] = true; array_push($_session["error_messages"], "email given registered."); } # handle duplicate username else if (strpos(mysqli_error($con), "username_unique") !== false ) { $_session["errors_found"] = true; array_push($_session["error_messages"], "username taken."); } # handle other sql query error... else { die("database query error: " . mysqli_error($con)); } }

first of all, wouldn't phone call such process "handling mysqli errors", rather "user input validation". , secondly, wouldn't perform such validation way @ all.

i've been under impression validating user input against database performed using select queries, not insert ones. not create application error-free, create more flexible, allowing separate verifications ajax-based registration example.

as real mysqli errors - have handle them, instead of dying. convenient yet easy way throwing exception:

if (!mysqli_query($con, $sql)) { throw new exception(mysqli_error($con)); }

which handled according site-wide error handling settings , provide priceless trace log.

php error-handling mysqli

No comments:

Post a Comment