title: mysql: Using php, how does one catch the error when mysqli_connect() fails?
body: <p>Yes, this has been asked before. The other answers did not help.</p>
  
  <p>I am new to mysql and php.
  I am taking a college course and I have a html-php-mysql project in front of me.</p>
  
  <p>I have been coding for 30 years and working with relational databases for 20. So I have some experience. Yes, it is homework, but I have written a necessary bit of simple code that does not work.</p>
  
  <p>I feel the core issue is the class has no textbook(s) and we were instructed to "find all the help you needed on the web".
  As there are many ways to skin a cat with php, it has been a challenge to find examples that match those of my instructor. Finding deprecated mysql_* examples makes the search more difficult. And most of the code samples assume the queries will always work! And the db connects as well. This is the last bit. My php handles all the other errors I can throw at it.</p>
  
  <p>This snippet is a frankenstein, but I have not gotten this to work. Bear with me and the code.</p>
  
  <p>No doubt there are many things one should do to make this production code, but that is not the goal. I am trying to get toy html files and php scripts to connect to a toy mysql database and produce a near-trivial result. All those considerations are not germane.</p>
  
  <p>I am forcing the connect to fail so my code can gracefully tell the user the system is down and I can log the error.</p>
  
  <p>A php error is thrown as "rootx" is invalid.</p>
  
  <pre><code>"PHP Warning:  mysqli_connect(): (HY000/1045): Access denied for user 'rootx'@'localhost'"
  </code></pre>
  
  <p>But the error is not caught, "catch" is skipped and the "finally" clause runs.</p>
  
  <pre><code>mysqli_report(MYSQLI_REPORT_STRICT);
  </code></pre>
  
  <p>was added as this line fixed this identical problem. It did not help.</p>
  
  <p>Might it be how wamp is configured? All I did was install on this windows 10 box as I was directed by the installer. </p>
  
  <pre><code>mysqli_report(MYSQLI_REPORT_STRICT);
  try {
      $conn = mysqli_connect( 'localhost', 'rootx', '',  'csc443' );
                            // server,     username, pw, dbname
      }
  } catch( Exception $e ) {
      log_action( "dbconnectfail " . $username . " | " . $e-&gt;getCode() . " | " . $e-&gt;getMessage() );
      header( 'Location: 500.php' );
      return;    
  } finally {
      log_action( "dbconnect localhost::csc443" );
  }
  </code></pre>