Check for database connection, otherwise display message

162,935

Solution 1

Try this:

<?php
$servername   = "localhost";
$database = "database";
$username = "user";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}
  echo "Connected successfully";
?>

Solution 2

very basic:

<?php 
$username = 'user';
$password = 'password';
$server = 'localhost'; 
// Opens a connection to a MySQL server
$connection = mysql_connect ($server, $username, $password) or die('try again in some minutes, please');
//if you want to suppress the error message, substitute the connection line for:
//$connection = @mysql_connect($server, $username, $password) or die('try again in some minutes, please');
?>

result:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: YES) in /home/user/public_html/zdel1.php on line 6 try again in some minutes, please

as per Wrikken's recommendation below, check out a complete error handler for more complex, efficient and elegant solutions: http://www.php.net/manual/en/function.set-error-handler.php

Share:
162,935
Chriswede
Author by

Chriswede

Updated on July 09, 2022

Comments

  • Chriswede
    Chriswede almost 2 years

    I would like to check if the website can connect to mySQL. If not, I would like to display an error saying that the user should try to access the page again in a few minutes...

    I really do not know how to do this ;)

    Any help would be greatly appreciated!

    string mysql_error ([ resource $link_identifier ] )
    

    But how do I use this?

    This just gives me the error, but I want the message to display with any error.

    Thanks

  • Wrikken
    Wrikken over 12 years
    Please, do not die..... Echo an error message, but the use trigger_error('some internal error message',E_USER_ERROR);, that way, the error shows up in your logs, as you are unlikely to be the one to catch it yourself on a busy site. Of course, this also assumes log_errors to be on, and display_errors to be off, which should be the default for any production environment.
  • Chriswede
    Chriswede over 12 years
    Can I also place php code inside the "{ // place error here }" because this give me an error
  • Chriswede
    Chriswede over 12 years
    so I could replace code : echo "Please try later." with code : <?php $fade_amount = 60; //In Percentage $box_width = 400; $box_background = 'FFFFFF'; //Hex Color $box_border_width = 1; $box_border_color = '999999'; $close_box = 1; ... Thanks for your help!!!! +1
  • psyklopz
    psyklopz over 12 years
    Remove the <?php and ?> tags from your stuff. Also, I would remove the die() function. I also refined it again to wrap the success stuff in the else block.
  • Pathros
    Pathros over 5 years
    Nice! This could lead me to this connection error solution: mysqli_real_connect(): The server requested authentication method unknown to the client
  • Jason Swett
    Jason Swett about 5 years
    How is this supposed to work if $database is never used?
  • psyklopz
    psyklopz almost 5 years
    @JasonSwett - When this was edited to support the new mysqli library, it looks like that information was dropped. I just edited it back to pass the database as part of the constructor.
  • psyklopz
    psyklopz almost 5 years
    And for the record, calling die() should be avoided at all costs. Replace that with your own error handling logic.