drxkx6149 2017-03-16 08:22
浏览 184
已采纳

new mysqli - 为什么会显示错误消息

I want to check if accessing mysql is possible or not.

I don't wont to have an error message, only true/false if the connection is ready or not.

I tried to do it with this :

$appDB = new mysqli($data[0]['MYSQL']['HOST'], $data[0]['MYSQL']['BENUTZER'], $data[0]['MYSQL']['PW'], $data[0]['MYSQL']['TABELLE']);
  if ($appDB->connect_error) 
  { $res['code']=FALSE; } 
  else 
  { 
    $appDB -> close();
    $res['code']=TRUE; 
  }

But always a error message will come up - how can i prevent this message from showing ?

"<b>Fatal error</b>:  Uncaught exception 'mysqli_sql_exception' with message 'Access denied for user ''
@'localhost' (using password: NO)' in /is/htdocs/...mypath
.php:33
Stack trace:
#0 /is/htdocs...mypath.myfile.php(33): mysqli-&gt;mysqli('', ''
, '', '')
#1 {main}
  thrown in <b>/is/htdocs/wp1076647_373QG1K1B0/butobo/module/4/code/cms_checkcon.php</b> on line <b>33
</b><br />"
  • 写回答

2条回答 默认 最新

  • doutuan6158 2017-03-16 08:27
    关注

    You need to "catch" this fatal exception via a try/catch statement. This allows you to handle the fatal error and then instead of your script crashing due to it, you can output an error message and exit instead :D

    PHP Exceptions: http://php.net/manual/en/language.exceptions.php

    I hope this helps.

    try
    {
        $appDB = new mysqli($data[0]['MYSQL']['HOST'], $data[0]['MYSQL']['BENUTZER'], $data[0]['MYSQL']['PW'], $data[0]['MYSQL']['TABELLE']);
          if ($appDB->connect_error) 
          { $res['code']=FALSE; } 
          else 
          { 
            $appDB -> close();
            $res['code']=TRUE; 
          }
    }
    catch (Exception $e)
    {
        echo 'Caught exception: ',  $e->getMessage(), "
    ";
        exit;
    }
    

    This will change the ERROR to a WARNING, which will not break your script. From there, if you want to hide everything EXCEPT fatal errors, you can use this line of PHP:

    error_reporting(E_ERROR);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求计算赫斯特(Hurst)指数
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大