title: How to check if user exists in database php
body: <p>For some reason when i enter an existing username and password into my form the output is ALWAYS 'user does not exist', how could i change it so that it will take the user to the welcome.php page if the user exists and 'user does not exist' if the user doesn't. The database is called TEST and the table is called USERS.</p>
  
  <pre><code>&lt;?php 
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "test";
  
  // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      }
  
      $user = $conn-&gt;real_escape_string($_POST['username']);
      $pass = $conn-&gt;real_escape_string($_POST['password']);
  
      $query = "SELECT `username` AND `password` FROM `users` WHERE `username` = '$user' AND `password` = '$pass'";
  
      $result = $conn-&gt;query($query);
      if($result-&gt;num_rows &lt; 0) {
  
         header('Location:welcome.php');
         die();
      }
      else $message = 'User does not exist';
  ?&gt;
  
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
          &lt;head&gt;
              &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
              &lt;title&gt;Test&lt;/title&gt;
          &lt;/head&gt;
  
          &lt;body&gt;
              &lt;form method="post" action="testing.php" enctype="multipart/form-data"&gt;
                  &lt;?php if(isset($message)) : ?&gt;
                      &lt;div class="error"&gt;&lt;?php echo $message; ?&gt;&lt;/div&gt;
                  &lt;?php endif; ?&gt;
  
                  Username: &lt;input type="text" value="" name="username" /&gt;&lt;br /&gt;&lt;br /&gt;
                  Password: &lt;input type="text" value="" name="password" /&gt;&lt;br /&gt;&lt;br /&gt;
                  &lt;input type="submit" name="submit" value="Log In"/&gt;&lt;br /&gt;&lt;br /&gt;
                  &lt;a href="register.php"&gt;Register&lt;/a&gt;
              &lt;/form&gt;
          &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>