有什么方法可以捕获 PHP 中的 MySQL 和数据库错误吗?
有时我会收到数据库错误,例如
警告:mysql_connect() [function.mysql-connect]:用户“test”@“101.190.193.83”访问被拒绝(使用密码:YES)
无法连接:用户“test”@“101.190.193.83”的访问被拒绝(使用密码:YES)”
但实际上密码没有更改。
有没有办法在日志文件中捕获此错误并在屏幕上显示一些不错的消息,例如“服务器错误。请过一段时间再试一次。”
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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不希望 PHP 显示警告,则必须使用“@”运算符
您还可以考虑设置 display_errors 到 0 在生产中的
php.ini
文件中您也可以考虑 PDO 用于连接MySQL,它默认使用异常来报告错误,
If you don't want PHP to show the warning, you have to use the "@" operator
You may also consider setting display_errors to 0 in your
php.ini
file in productionYou may also consider PDO for connecting to MySQL, it's using exceptions as a default to report errors,
是的。所有 PHP 错误和警告都会写入 Web 服务器的错误日志文件中,因此您无需执行任何其他操作 - 它已经完成了。
要回答问题的第二部分,如果您不希望在屏幕上显示原始错误消息,可以通过以下两种方法之一来防止这种情况:
在前面使用
@
符号你的函数调用——即$db = @mysql_connect(...);
。这将仅针对该特定函数调用关闭错误报告。通常认为过度使用此技术是一个坏主意,但偶尔这样做是合法的。更好的选择可能是在您的
PHP.ini
、本地.htaccess
文件中或使用关闭全局错误报告标志程序中的 ini_set()
。通常,网页上的错误报告只能在开发网站时使用。网站上线后,您应该开启错误报告,这样您就不会在客户面前精心构建的页面布局中随机出现 PHP 错误。发生的任何错误仍会写入服务器错误日志,但不会显示在页面上。
对于 MySQL 错误(例如您遇到的错误),您仍然可以使用
mysql_error()
函数在程序中获取错误本身。这将包含最后发生的错误的详细信息,因此您可以通过编程方式检查它并报告合理的错误消息。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:
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.The better option may be to turn the global error reporting flag off, either in your
PHP.ini
, in your local.htaccess
file, or usingini_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.来源:http://wallstreetdeveloper.com/php-database-connection/
这是一个示例php中数据库连接的代码:
Source : http://wallstreetdeveloper.com/php-database-connection/
Here is a sample code for database connectivity in php: