title: Function to block duplicate usernames in database when true, inserts names into database when true
body: <p>I made this function to prevent duplicate usernames in my mysql database when registering. If it returns false then the name can be made:</p>
  
  <pre><code>public function nameTaken($username){
  
      $statement = $this-&gt;pdo-&gt;prepare('SELECT count(*) FROM users WHERE name = :name');
  
      $statement-&gt;execute(array('name' =&gt; $username));
  
      $res = $statement-&gt;fetch(PDO::FETCH_NUM);
  
      $exists = array_pop($res);
  
      if ($exists &gt; 0) {
  
          echo 'user already exists';
  
          return;
  
      } else {
          //the name can be made
          return false;
      }
  }
  </code></pre>
  
  <p>When I tested it, even when it echo'd that users already exist and didn't return false, the username from the post request were still inserted into my database. Here is my function to insert into my database :</p>
  
  <pre><code>public function insert($table, $parameters){
  
      $sql = sprintf(
  
          'insert into %s (%s) values (%s)',
  
          $table,
  
          implode(', ', array_keys($parameters)),
  
          ':' . implode(', :', array_keys($parameters))
  
      );
  
      try {
          $statement = $this-&gt;pdo-&gt;prepare($sql);
  
          $statement-&gt;execute($parameters);
  
      }   catch (Exception $e) {
  
          die('something went wrong');
  
      }
  
  }
  </code></pre>
  
  <p>and here is my controller that gets the post requests to to register a name </p>
  
  <pre><code>&lt;?php
  
  $hash = password_hash($_POST['password'], PASSWORD_BCRYPT);
  
  if(!$app['database']-&gt;nameTaken($_POST['username'])){
  
      $app['database']-&gt;insert('users', [
  
          'name' =&gt; $_POST['username'],
  
          'password' =&gt; $hash
  
      ]);
  };
  </code></pre>