title: How to prevent/catch connection error with MySQLi
body: <p>How do you prevent a connection error if, for example, the database name is invalid, for the given code below?</p>
  
  <pre><code>$mysql = new mysqli('localhost', 'user', 'pass', 'wrongdatabase');
  
  if($mysql-&gt;connect_errno)
      die($mysql-&gt;connect_error);
  </code></pre>
  
  <p>The if statement will output the right error message, but there will still be a warning sent from the first line, which says</p>
  
  <pre><code>Warning: mysqli::mysqli() [&lt;a href='mysqli.mysqli'&gt;mysqli.mysqli&lt;/a&gt;]: (42000/1049): Unknown database 'wrongdatabase' in C:\wamp\www\example.php on line 14
  </code></pre>
  
  <p>I know with PDO you would simply wrap it in a try/catch block, but how to do it with MySQLi?</p>