0

Using the typical MySQL connection method, if the connection fails (ex. bad credentials), I'd like the user to be notified with a nice redirect with error message, instead of a plain text Fatal Error message with technical jargon.

$db = new MySQLi($hostname, $username, $password, $database);
if ($db->connect_error) {
   echo "Error: ".$db->connect_error;
   die();
}

Shows a plain text page with an error message, if I have error_reporting on. What I'd like to do is something like:

if ($db->connect_error) {
   // Failed
   header('location: ../setup_db.php?connect_error=1');
   die();
} else {
   // Connected. Do stuff...
}

Unfortunately, I cannot get PHP to get past the failed connection error that quits the program. Is there a way to get a signal from MySQL or PHP that the connection won't work but continue to run the program?

For further info, the error printed currently is not a printout of $db->error_connect, but a PHP error:

Fatal error: Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for...

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • What are your options? If MySql report strict is enabled, it will return an error. – JoelCrypto Jan 20 '23 at 17:13
  • 1
    You should never redirect in case of error for starter – Your Common Sense Jan 20 '23 at 17:41
  • @YourCommonSense What's the reason why? Edit: What's better instead of redirecting? – liminalFrog Jan 20 '23 at 17:48
  • 1
    HTTP standard says so. In case of error the status code returned must be 500, not 302. Neither a redirect is required to show "a nice error message", [PHP has error handlers for that](https://phpdelusions.net/articles/error_reporting#error_page). And on top of that, a database connection code should never interact with a user. That's a big fault architecturally-wise. There must be just a single piece of code that tells a user that something went wrong, be it a database connection or a syntax error. – Your Common Sense Jan 20 '23 at 17:55
  • So if I convert the error (made by bad credentials) to an exception and added code to the `catch`, I could put a `header('location: ...')` to redirect? Or am I still breaking the rules? Basically, am I in the clear if I convert an error to an exception? – liminalFrog Jan 20 '23 at 18:19
  • Why do you want to redirect the user anywhere at all? – Dharman Jan 20 '23 at 18:50
  • Why would you need to convert if it's already converted? Exception is an error all the same, just more convenient. How it's even relevant here? You have an error connecting to database. **It remains an error**, whatever form it is takes - a warning, an exception or a fatal error. Why you're haggling tho? Why keep that redirect? – Your Common Sense Jan 20 '23 at 18:54
  • @Dharman Trying to make a form for entering database information so the program can create tables. If the user enters the info wrong, I want him to be alerted. I usually just redirect to the original form with a ?err=1 that triggers a message, like "This field is required." But what about incorrect database information? – liminalFrog Jan 20 '23 at 21:02
  • Database credentials should never come from user input. The only real scenario I can think of when this could happen is if you were designing software like phpMyAdmin. In that case, your only option is to try to connect and catch the exception if it doesn't work. Redirects on error conditions are a bad design. I'd recommend reading about different design patterns. Appending `?err=1` to a URL is a code smell and a poor experience for your users. But that's a whole other topic. – Dharman Jan 20 '23 at 21:17
  • @Dharman That's actually pretty close to what I'm doing. Basically a system where there's an initial setup and the Admin can input database info at setup. After which, that input would be stored in a config file outside the web root dir – liminalFrog Jan 20 '23 at 21:47

0 Answers0