title: Best way to check for existing user in mySQL database?
body: <p>I am trying to create a user login/creation script in PHP and would like to know the best way to check if a username exists when creating a user. At the moment, I have the following code:</p>
  
  <pre><code>function createUser($uname,$pword) {
          $server-&gt;connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
          $this-&gt;users = $server-&gt;query("SELECT * FROM user_list");
          while ($check = mysql_fetch_array($this-&gt;users) {
              if ($check['uname'] == $uname) {
  </code></pre>
  
  <p>What I'm not sure about is the best logic for doing this. I was thinking of adding a boolean variable to do something like (after the if statement):</p>
  
  <pre><code>$boolean = true;
  }
  if ($boolean) {
      echo "User already exists!";
      }
  else {
      $server-&gt;query("INSERT USER INTO TABLE");
      echo "User added Successfully";
      }
  </code></pre>
  
  <p>But this seems a little inefficient - is there a more efficient way to do this? Sorry if this has a basic solution - I'm a relatively new PHP programmer.</p>