2

I'm having an issue yesterday and today with my host (godaddy) who apparently is having trouble with a particular mysql server... in that it's completely out of commission for a few hours at a time.

Right now this causes my site to not load at all - showing this error:

[phpBB Debug] PHP Warning: in file /home/content/index.php on line 53: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'dbserver.com' (110)
Connect failed: Can't connect to MySQL server on 'dbserver.com' (110)

I'm guessing it's showing that because of this code I have:

// PREPARE DB CONNECTION
$mysqli = new mysqli("dbserver.com", "username", "password", "dbname");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Which tells the php to stop altogether if mysql is down. So I suppose all I need to do is remove the "exit" function and I'm good to go?

I guess theoretically speaking, why is exit there by default in so many tutorials of how to connect to mysql? It kills the web site if the database is unavailable.

Just looking for thoughts from those who know much more than I please, thank you!

6
  • 3
    Will the rest of your site work at all if you can't connect to the database?
    – Jessica
    Dec 27, 2013 at 19:21
  • 7
    Remember not to post any sensitive data like username or password. Now go change them on your server. Dec 27, 2013 at 19:22
  • Do you use any caching mechanism? like memcached?
    – Ibu
    Dec 27, 2013 at 19:24
  • @Jessica Actually the majority of it would not work right. :(
    – runelynx
    Dec 27, 2013 at 19:33
  • And don't forget to change your database password since you post it on a public site with thousands views a day! Dec 27, 2013 at 19:42

3 Answers 3

5

Just bin this:

printf("Connect failed: %s\n", mysqli_connect_error());

And replace it with a placeholder page instead to inform people that s**t happened and you'll be back later.

header('Location: error_db.php');
4
  • This won't stop the rest of the page code from execution and might not be what you want.
    – initall
    Dec 27, 2013 at 19:26
  • 1
    Should always have a die(); or exit(); after a header redirect.
    – Jessica
    Dec 27, 2013 at 19:49
  • As stated more than once above it is the common mistake to forget exit or die after header redirects. Anyone using this type of redirects should exit their currently running code if otherwise specifically intended.
    – Madcoe
    Jan 31, 2014 at 5:42
  • @Madcoe: And if you read his existing code, you'll see that there is already an exit() following the line I suggested that he replaces. So where's the problem? Jan 31, 2014 at 8:41
1

Displaying good and pretty error-messages to the user is often neglected by most programmers. This is because it can be a real pain in the ass to do the right way.

As Jassica commented, if your entire site function without the database-connection, you can just remove the exit() in your function and the site is good to go.

However, I guess the chances for a 100 % functional site without the database-connection are very small. What most sites to is to redirect the user to a 500-errorpage where they inform the user that something went wrong. Often provided contact-information so the right people can be notified too. Twitter does this, so does Facebook, Reddit and many more.

You can just do:

$mysqli = new mysqli("dbserver.com", "username", "password", "table");
if (mysqli_connect_errno()) {
    header("Location: error500.html");
    exit();
}

Or something like that.

1

This should do the trick.

function connect($u,$p,$db,$host) { 
   try { 
      $mysqli = new mysqli($host,$u,$p,$db); 
      $connected = true; 
   } catch (mysqli_sql_exception $e) { 
      throw $e; 
   } 
} 

try { 
  connect('username','password','database','host');  
} catch (Exception $e) { 
  echo $e->errorMessage(); 
} 

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.