title: Check if connected to database (right username/password, host)
body: <p>I would love to check if i can connect to database using given username, password, and database host.</p>
  
  <p><strong>So far i was trying:</strong></p>
  
  <pre><code>$conn = mysql_connect($dbhost, $dbuser, $dbpass);
  $conndatabase = mysql_select_db($dbname);
  
  if(!mysql_ping($conn) || !$conndatabase){ 
      //do something when cant connect 
  } else { 
      //do something when you connected
  }
  </code></pre>
  
  <p>It works when i give bad <code>$dbname</code>, cuz it cant select this database. But when i give wrong host, username or password, it will give me white page with errors. For example:</p>
  
  <pre><code>Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'rootx'@'localhost' (using password: YES) in C:\xampp\htdocs\strony\planer\config\opendb.php on line 6
  Warning: mysql_ping() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\strony\planer\grupy.php on line 3
  </code></pre>
  
  <p><strong>Question:</strong><br>
  Is there  a way to check if i can connect using given data without errors? It just gives errors before getting to <code>mysql_ping()</code></p>
  
  <p><strong>EDIT</strong><br>
  After trying Mike Brant solution, i think i could have explained it wrong. I am trying to connect to database, and if it lets me, it shows user page he wanted to access, but when it's impossible, it redirects him to the other page, to modify database information.</p>
  
  <pre><code>if(!$conndatabase){
      header('Location: index.php');
  }
  </code></pre>
  
  <p>I am using that for checking if database exists. But if i try <code>if(!$conn)</code> or something similar, its too late, because i got errors displayed on <code>$conn = mysql_connect($dbhost, $dbuser, $dbpass)</code>. So i cant redirect, as it gives me  </p>
  
  <pre><code>Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost'~~
  Warning: Cannot modify header information - headers already sent by~~
  </code></pre>