php mysql_connect 警告禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16912899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:52:36  来源:igfitidea点击:

php mysql_connect Warning disable

phpmysql

提问by Gayane

I have php script wich should try to connect to DB in local site. If the local DB is not available it should try to connect to DB on remote server.

我有 php 脚本,应该尝试连接到本地站点中的数据库。如果本地数据库不可用,它应该尝试连接到远程服务器上的数据库。

$dblink = mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS) or $RC = 1;
if($RC) {
    $dblink = mysql_connect(DBHOST_REMOTE, DBUSER, DBPASS) or die('Could not connect'.mysql_error());
}

The problem is that I don't want to display Warning message on page if connection faild the first time. Is there any way to disable the warning message only for mysql_connect() function?

问题是如果第一次连接失败,我不想在页面上显示警告消息。有什么方法可以仅针对 mysql_connect() 函数禁用警告消息?

回答by Software Guy

Yes, add an @ sign like so to suppress warning / error messages, then do the error onceyour own:

是的,像这样添加一个@ 符号来抑制警告/错误消息,然后自己做一次错误:

$dblink = @mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS);

if (!$dblink) 
{
    $dblink = @mysql_connect(DBHOST_REMOTE, DBUSER, DBPASS);                  
}

if (!$dblink)
{
    $message = sprintf(
        "Could not connect to local or remote database: %s",
        mysql_error()
    );
    trigger_error($message);
    return;
}

Take care that you need to handle all error reporting your own then. Such code is hard to debug in case you make a mistake.

请注意,您需要自己处理所有错误报告。如果你犯了错误,这样的代码很难调试。

回答by liyakat

you can set error reporting error_reporting(0);to turn off errors and warning for speacific page

您可以设置错误报告error_reporting(0);以关闭特定页面的错误和警告

just set as per your need

只需根据您的需要设置

# config.ini 
# PHP error reporting. supported values are given below. 
# 0 - Turn off all error reporting 
# 1 - Running errors 
# 2 - Running errors + notices 
# 3 - All errors except notices and warnings 
# 4 - All errors except notices 
# 5 - All errors 

Doc

文件

just set at head in page like this

就像这样设置在页面的头部

<?php 

      error_reporting(E_ERROR | E_WARNING | E_PARSE);

?>

let me know if i can help you more.

如果我能帮助你更多,请告诉我。

回答by MrCode

The proper solution is to disable the displaying of PHP errors, warnings and notices to the user. This can be done via the display_errorssetting in your php.ini configuration file:

正确的解决方案是禁止向用户显示 PHP 错误、警告和通知。这可以通过display_errorsphp.ini 配置文件中的设置来完成:

display_errors = Off

It can also be set at runtime via ini_set():

它也可以在运行时通过ini_set()以下方式设置:

ini_set('display_errors', '0');

From the Manual:

手册

display_errors
This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

display_errors
这决定了错误是否应该作为输出的一部分打印到屏幕上,或者是否应该对用户隐藏。

Doing that would prevent any users from seeing sensitive PHP error messages, but still allow them to be printed to the log file.

这样做会阻止任何用户看到敏感的 PHP 错误消息,但仍允许将它们打印到日志文件中。

Use of the @suppression method would do what you want but it would still allow any other PHP errors/warnings to be printed to the user, which is a security issue because those errors can contain sensitive information that should not be exposed.

使用@抑制方法可以执行您想要的操作,但它仍然允许向用户打印任何其他 PHP 错误/警告,这是一个安全问题,因为这些错误可能包含不应公开的敏感信息。

回答by Mahdi Shad

I put mysql queries in trysyntax to avoid displaying messages

我将 mysql 查询放在try语法中以避免显示消息

try {
    some code of sql
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}

回答by Benz

Can't you use the following method:

你不能用下面的方法:

if (!$connection = mysql_connect(DBHOST_LOCAL, DBUSER, DBPASS)) {
    die('And here we connect to the other server');
} 

When the connection fails, it returns a boolean, which will cause the if statement to be true.

当连接失败时,它返回一个布尔值,这将导致 if 语句为真。