Is there any way to catch MySQL and database errors in PHP?

久未见 提交于 2020-01-01 08:46:42

问题


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:


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();
}



回答2:


<?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'; }
?>



回答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.




回答4:


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);
?>



回答5:


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.';
}


来源:https://stackoverflow.com/questions/6120589/is-there-any-way-to-catch-mysql-and-database-errors-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!