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
This error means that your website files (on the webserver) are not able to connect to your database (on the database server). This article lists some common reasons this error could display on your site, including: Incorrect database credentials in your config file. The hostname isn't working.
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";
?>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With