title: Check if username exists PDO
body: <p>How would I be able to check multiple factors combined instead of checking for each one? So basically I'm using PDO and I have to make sure that the usernames and emails are unique. So how would I do that? I've seen </p>
  
  <pre><code>if ( $sthandler-&gt;rowCount() &gt; 0 ) {
  // do something here
  }  
  </code></pre>
  
  <p>But is there a better way to do it. Also if there isn't can someone explain how I'd work with that. </p>
  
  <p><strong>EDIT</strong>
  Here's my query code that inputs into the database </p>
  
  <pre><code>        &lt;?php
  try {
      $handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
      $handler-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e){
      exit($e-&gt;getMessage());
  }
  
  $name = $_POST['name']; 
  $username = $_POST['username']; 
  $email = $_POST['email'];   
  $password = $_POST['password']; 
  $password1 = $_POST['passwordconf'];
  $ip = $_SERVER['REMOTE_ADDR'];
  
  //Verifcation 
  if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password1))
      {
      echo "Complete all fields";
      }
  
  // Password match
  if ($password != $password1)
      {
      echo $passmatch = "Passwords don't match";
      }
  
  // Email validation
  
  if (!filter_var($email, FILTER_VALIDATE_EMAIL))
      {
      echo $emailvalid = "Enter a  valid email";
      }
  
  // Password length
  if (strlen($password) &lt;= 6){
      echo $passlength = "Choose a password longer then 6 character";
  }
  
  
  
  
  
  function userExists($db, $user)
  {
      $userQuery = "SELECT * FROM userinfo u WHERE u.user=:user;";
      $stmt = $db-&gt;prepare($userQuery);
      $stmt-&gt;execute(array(':user' =&gt; $user));
      return !!$stmt-&gt;fetch(PDO::FETCH_ASSOC);
  }
  
  $user = 'userName';
  $exists = userExists($db, $user);
  if(exists)
  {
       // user exists already.
  }
  else
  {
       // user doesn't exist already, you can savely insert him.
  }
  if(empty($passmatch) &amp;&amp; empty($emailvalid) &amp;&amp; empty($passlength)) {
  
  //Securly insert into database
  $sql = 'INSERT INTO userinfo (name ,username, email, password, ip) VALUES (:name,:username,:email,:password,:ip)';    
  $query = $handler-&gt;prepare($sql);
  
      $query-&gt;execute(array(
  
      ':name' =&gt; $name,
      ':username' =&gt; $username,
      ':email' =&gt; $email,
      ':password' =&gt; $password,
      ':ip' =&gt; $ip
  
      ));
      }
  ?&gt;
  </code></pre>