title: PHP mysqli: how to detect access denied or bad parameters when connecting to database
body: <p>I have the following code:</p>
  
  <pre><code>// Connect to DB
  
  $mysqli = new mysqli( $host, $user, $password, $database );
  if( ! $mysqli )
  {
      echo "Unable to connect to database\n";
      exit(1);
  }
  </code></pre>
  
  <p>If the password provided or the host name/address is invalid the mysqli object is instantiated anyway. <code>! $mysqli</code> is evaluated <code>false</code> so the connection failure is not detected.</p>
  
  <p>If I check <code>$mysqli-&gt;error</code> I get no error.</p>
  
  <p>What happens is that PHP (I'm using PHP 5.6 from the command line) is raising warnings. Then what fails is the first query or any other attempt to operate with the (failed) connection.</p>
  
  <p>What is the best way to suppress warnings and catch the connection failure into the code?</p>