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条)

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决