有没有办法在PHP中捕获MySQL和数据库错误?

时间:2022-10-13 13:48:05

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)

警告:mysql_connect()[function.mysql-connect]:拒绝访问用户'test'@'101.190.193.83'(使用密码:YES)

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

无法连接:访问被拒绝用户'test'@'101.190.193.83'(使用密码:是)“

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."

有没有办法在日志文件中捕获此错误并在屏幕上显示一些不错的消息,如“服务器错误。请再试一次。”

5 个解决方案

#1


10  

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

如果您不希望PHP显示警告,则必须使用“@”运算符

$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

您还可以考虑在生产中的php.ini文件中将display_errors设置为0

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

您也可以考虑将PDO连接到MySQL,它使用异常作为默认报告错误,

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Could not connect : ' . $e->getMessage();
}

#2


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

#3


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.

是。所有PHP错误和警告都写入Web服务器的错误日志文件,因此您无需执行任何其他操作 - 它已经完成。

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.

    在函数调用前使用@符号 - 即$ db = @mysql_connect(...);.这将仅针对该特定函数调用关闭错误报告。过度使用这种技术通常被认为是一个坏主意,但偶尔做它是合法的。

  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.

    更好的选择可能是关闭全局错误报告标志,可以在PHP.ini中,在本地.htaccess文件中,也可以在程序中使用ini_set()。

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.

通常,只有在您开发网站时才应使用网页上的错误报告。一旦网站上线,您应该打开错误报告,这样您就不会在客户面前精心构建的页面布局中随机出现PHP错误。发生的任何错误仍将写入服务器错误日志,但不会显示在页面上。

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.

对于MySQL错误,例如您已经获得的错误,您仍然可以通过使用mysql_error()函数来解决程序中的错误。这将包含要发生的最后一个错误的详细信息,因此您可以通过编程方式检查它并报告合理的错误消息。

#4


1  

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

资料来源:http://wallstreetdeveloper.com/php-database-connection/

Here is a sample code for database connectivity in php:

以下是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


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

#1


10  

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

如果您不希望PHP显示警告,则必须使用“@”运算符

$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

您还可以考虑在生产中的php.ini文件中将display_errors设置为0

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

您也可以考虑将PDO连接到MySQL,它使用异常作为默认报告错误,

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Could not connect : ' . $e->getMessage();
}

#2


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

#3


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.

是。所有PHP错误和警告都写入Web服务器的错误日志文件,因此您无需执行任何其他操作 - 它已经完成。

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.

    在函数调用前使用@符号 - 即$ db = @mysql_connect(...);.这将仅针对该特定函数调用关闭错误报告。过度使用这种技术通常被认为是一个坏主意,但偶尔做它是合法的。

  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.

    更好的选择可能是关闭全局错误报告标志,可以在PHP.ini中,在本地.htaccess文件中,也可以在程序中使用ini_set()。

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.

通常,只有在您开发网站时才应使用网页上的错误报告。一旦网站上线,您应该打开错误报告,这样您就不会在客户面前精心构建的页面布局中随机出现PHP错误。发生的任何错误仍将写入服务器错误日志,但不会显示在页面上。

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.

对于MySQL错误,例如您已经获得的错误,您仍然可以通过使用mysql_error()函数来解决程序中的错误。这将包含要发生的最后一个错误的详细信息,因此您可以通过编程方式检查它并报告合理的错误消息。

#4


1  

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

资料来源:http://wallstreetdeveloper.com/php-database-connection/

Here is a sample code for database connectivity in php:

以下是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


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