PHP中未显示预期的异常消息

R K*_*R K 15 php mysql

我想在PHP中编写一个带有数据库连接和异常处理的程序.如果我插入一个不正确的用户名,那么它应该显示相应的错误消息,如果我插入不正确的数据库,它应该显示相应的错误消息.

但是在下面的程序中,无论是插入不正确的数据库还是不正确的用户名,它都只显示消息"无法连接到数据库".

<?php
   $hostname = "localhost";
   $username = "root1";
   $password = "";
   $database = "php_thenewboston";
   $conn = mysqli_connect($hostname,$username,$password);
   $conn_db = mysqli_select_db($conn,$database);
   class ServerException extends Exception{}  
   class DatabaseException extends Exception{}
   try{
       if(!$conn){
           throw new ServerException('Could not connect to server.');
       }elseif(!$conn_db){
           throw new DatabaseException('Could not connect to database.');
       }else{
           echo "Connected.";
       }
   }catch(ServerException $ex){
       echo "Error :".$ex->getMessage();        
   }catch(DatabaseException $ex){
       echo "Error :".$ex->getMessage();
   }
?>
Run Code Online (Sandbox Code Playgroud)

我是PHP的初学者.如有任何疑问,请在下面评论.

编辑

正如@Hatef所要求的那样var_dump,$conn当用户名不正确,密码正确且数据库名称正确时

object(mysqli)#1 (19) {
  ["affected_rows"]=>
  int(-1)
  ["client_info"]=>
  string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $"
  ["client_version"]=>
  int(50012)
  ["connect_errno"]=>
  int(0)
  ["connect_error"]=>
  NULL
  ["errno"]=>
  int(1044)
  ["error"]=>
  string(68) "Access denied for user ''@'localhost' to database 'php_thenewboston'"
  ["error_list"]=>
  array(1) {
    [0]=>
    array(3) {
      ["errno"]=>
      int(1044)
      ["sqlstate"]=>
      string(5) "42000"
      ["error"]=>
      string(68) "Access denied for user ''@'localhost' to database 'php_thenewboston'"
    }
  }
  ["field_count"]=>
  int(0)
  ["host_info"]=>
  string(20) "localhost via TCP/IP"
  ["info"]=>
  NULL
  ["insert_id"]=>
  int(0)
  ["server_info"]=>
  string(21) "5.5.5-10.1.16-MariaDB"
  ["server_version"]=>
  int(50505)
  ["stat"]=>
  string(132) "Uptime: 1072  Threads: 1  Questions: 16  Slow queries: 0  Opens: 18  Flush tables: 1  Open tables: 11  Queries per second avg: 0.014"
  ["sqlstate"]=>
  string(5) "00000"
  ["protocol_version"]=>
  int(10)
  ["thread_id"]=>
  int(9)
  ["warning_count"]=>
  int(0)
}
Run Code Online (Sandbox Code Playgroud)

以下是$conn用户名正确,密码正确且数据库名称不正确的var_dump .

object(mysqli)#1 (19) {
  ["affected_rows"]=>
  int(-1)
  ["client_info"]=>
  string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $"
  ["client_version"]=>
  int(50012)
  ["connect_errno"]=>
  int(0)
  ["connect_error"]=>
  NULL
  ["errno"]=>
  int(1049)
  ["error"]=>
  string(36) "Unknown database 'php_thenewboston1'"
  ["error_list"]=>
  array(1) {
    [0]=>
    array(3) {
      ["errno"]=>
      int(1049)
      ["sqlstate"]=>
      string(5) "42000"
      ["error"]=>
      string(36) "Unknown database 'php_thenewboston1'"
    }
  }
  ["field_count"]=>
  int(0)
  ["host_info"]=>
  string(20) "localhost via TCP/IP"
  ["info"]=>
  NULL
  ["insert_id"]=>
  int(0)
  ["server_info"]=>
  string(21) "5.5.5-10.1.16-MariaDB"
  ["server_version"]=>
  int(50505)
  ["stat"]=>
  string(132) "Uptime: 1417  Threads: 1  Questions: 18  Slow queries: 0  Opens: 18  Flush tables: 1  Open tables: 11  Queries per second avg: 0.012"
  ["sqlstate"]=>
  string(5) "00000"
  ["protocol_version"]=>
  int(10)
  ["thread_id"]=>
  int(10)
  ["warning_count"]=>
  int(0)
}
Run Code Online (Sandbox Code Playgroud)

Aga*_*nga 1

默认情况下,mysql会自动抛出异常。要手动抛出异常,您需要在文件顶部写入以下行,即使在 mysqli_connect 上,这也会抛出异常。所以你需要在try块中添加connect方法。 mysqli_report(MYSQLI_REPORT_STRICT);

更新:在您的情况下,不要编写上面的行,更改主机名,您将在 catch 块中收到服务器错误。