title: PHP - Check if the username or email is already taken
body: <p>I asked this question already (but it is with another question). Please refer to this <a href="https://stackoverflow.com/questions/38671619/php-and-mysql-check-if-username-is-already-taken/38671685?noredirect=1#comment64736862_38671685">thread</a> and see what I am talking about to. Please don't say that I should use PDO or improved MySQLi because I'm gonna change it as soon as I start learning about it.</p>
  
  <p>What if I want other fields to be "checked" if it already exists? My code is just working fine but it is just for the <em>username</em>, I also want to check if the Email Address is already taken or other fields too.</p>
  
  <p>My Code:</p>
  
  <pre><code>&lt;?php
      require_once("functions.php");
      require_once("db-const.php");
      session_start();
  
      if (logged_in() == true) {
          redirect_to("profile.php");
      }
  
      $errors=array();
  
  
  
      if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email'])) {
                  echo "Please fill all the fields!";
  
  }
                  elseif( isset( $_POST['username'], $_POST['password'], $_POST['first_name'], $_POST['last_name'], $_POST['email'] ) ) {
  
  
  
  
              $username   = !empty( $_POST['username'] ) ? $_POST['username'] : false;
              $mainpass   = !empty( $_POST['password'] ) ? $_POST['password'] : false;
              $password   = !empty( $mainpass ) ? hash('sha256', $mainpass) : false;
              $first_name = !empty( $_POST['first_name'] ) ? $_POST['first_name'] : false;
              $last_name  = !empty( $_POST['last_name'] ) ? $_POST['last_name'] : false;
              $email      = !empty( $_POST['email'] ) ? $_POST['email'] : false;
  
              if( $username &amp;&amp; $password ){
                  $mysqli = new mysqli(localhost, root, "", loginsecure);
                  if( $mysqli-&gt;connect_errno ) {
                      $errors[]=$mysqli-&gt;connect_error;
                  } else {
  
                      /* Assume all is ok so far */
                      $sql='select username from users where username=?';
                      $stmt=$mysqli-&gt;prepare($sql);
                      $stmt-&gt;bind_param('s',$username);
                      $stmt-&gt;execute();
  
                      $stmt-&gt;bind_result( $found );
                      $stmt-&gt;fetch();
  
                      if( !$found ){
                          /* username is not alreday taken */
  
                          $sql='insert into `users` (`username`,`password`,`first_name`,`last_name`,`email`) values (?,?,?,?,?);';
                          $stmt=$mysqli-&gt;prepare( $sql );
                          $stmt-&gt;bind_param('sssss',$username,$password,$first_name,$last_name,$email);
                          $stmt-&gt;execute();
  
                          header("Location: checklogin.php?msg=Registered Successfully!");
                      } else {
                          /* username is taken */
                          $errors[]='Sorry, that username is already in use.';
                      }
                  }
              }
          } else {
              $errors[]='Please fill in all details';
          }
      }
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Prospekt Member Area&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt; Register Here &lt;/h1&gt;
          &lt;h2&gt;&amp;copy; Kirk Niverba&lt;/h2&gt;
          &lt;hr /&gt;
  
          &lt;!-- The HTML registration form --&gt;
          &lt;form action="&lt;?=$_SERVER['PHP_SELF']?&gt;" method="post"&gt;
              Username: &lt;input type="text" name="username" /&gt;&lt;br /&gt;
              Password: &lt;input type="password" name="password" /&gt;&lt;br /&gt;
              First name: &lt;input type="text" name="first_name" /&gt;&lt;br /&gt;
              Last name: &lt;input type="text" name="last_name" /&gt;&lt;br /&gt;
              Email: &lt;input type="type" name="email" /&gt;&lt;br /&gt;
  
              &lt;input type="submit" name="submit" value="Register" /&gt;
              &lt;a href="login.php"&gt;Already have an account?&lt;/a&gt;
          &lt;/form&gt;
          &lt;?php
              if( !empty( $errors ) ){
                  echo implode( '&lt;br /&gt;', $errors );
              }
          ?&gt;
          &lt;hr /&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is not a <strong>duplicate question</strong>, the question in here is how to check it in <em>single query</em> not in <em>multiple queries</em>, and i'm also asking here if how do I use different error messages.</p>