9

Sometimes I am getting a database error like

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'test'@'101.190.193.83' (using password: YES)

Could not connect: Access denied for user 'test'@'101.190.193.83' (using password: YES)"

But truly there is no change in the password.

Is there any way to capture this error in a log file and show some nice message on the screen, like "Server error. Please try again some time."

1

5 Answers 5

10

If you don't want PHP to show the warning, you have to use the "@" operator

$connect = @mysql_connect(HOST, USER, PASS);//won't display the warning if any.
if (!$connect) { echo 'Server error. Please try again sometime. CON'; }

You may also consider setting display_errors to 0 in your php.ini file in production

You may also consider PDO for connecting to MySQL, it's using exceptions as a default to report errors,

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Could not connect : ' . $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

Comments

3
<?php
    $connect = mysql_connect(HOST, USER, PASS);
    if(!$connect) { echo 'Server error. Please try again sometime. CON'; }

    $select_db = mysql_select_db(DATABASE);
    if(!$select_db) { echo 'Server error. Please try again sometime. DB'; }
?>

2 Comments

The formatting is missing, appearantly stackoverflow doesn't recognize it.. pastebin.com/EFefnNSp
I fixed it for you. To get the code formatting, put 4 spaces (or 1 tab) before your code. You can do this easily by selecting the text and hitting CTRL+K.
3

Is there any way to capture these error in to log file ... ?

Yes. All PHP errors and warnings are written to the web server's error log file, so there's nothing else you need to do -- it's already being done.

To answer the second part of your question, if you don't want the raw error message displayed on the screen, you can prevent this in one of two ways:

  1. Use the @ symbol in front of your function call -- ie $db = @mysql_connect(...);. This will turn error reporting off just for that specific function call. It's generally considered to be a bad idea to over-use this technique, but it is a legitimate thing to do occasionally.

  2. The better option may be to turn the global error reporting flag off, either in your PHP.ini, in your local .htaccess file, or using ini_set() within the program.

Typically, error reporting on the web page should only be used while you're developing the site. Once the site goes live, you should turn error reporting, so that you don't get PHP errors showing up in random places in your carefully constructed page layout in front of your customers. Any errors that occur will still be written to the server error log, but won't be displayed on the page.

For MySQL errors, such as the one you've got, you can still get at the error itself within the program by using the mysql_error() function. This will contain the details of the last error to occurr, so you can programmaticaly check for it and report a sensible error message.

Comments

1

Source : http://wallstreetdeveloper.com/php-database-connection/

Here is a sample code for database connectivity in php:

<?php
//Step-1 : Create a database connection
$connection=mysql_connect(“localhost”,”root”,”root”);
if(!$connection) {
    die(“Database Connection error” . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db(“widget_corp”,$connection);
if(!$db) {
    die(“Database Selection error” . mysql_error());
}
?>
<html>
<head>
<title>Database</title>
</head>
<body>
<?php
 //Step 3 : Perform database Queury
 $result=mysql_query(“select * from subjects”,$connection);
if(!$result) {
    die(“No rows Fetch” . mysql_error());
}

//Step 4 : Use returned data
while($row=mysql_fetch_array($result))
{
     //echo $row[1].” “.$row[2].”<br>”;
    echo $row["menu_name"].” “.$row["position"].”<br>”;
}

?>
</body>
</html>
<?php
//Step 5 : Close Connection
mysql_close($connection);
?>

Comments

0
try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);// if your are not set this attributes you won't get any exceptions.
} catch (PDOException $e) {
    echo 'Server error. Please try again some time.';
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.