PHP and MySQL - Check if username is already taken
<p>The problem now is, the code couldn't check if the username is already taken, is there any code that can check the username is already taken in the database?</p>

<p>I am experimenting with some of my codes then probably searched it also in Stack Overflow about this issue. I've tried this <a href="https://stackoverflow.com/a/32277440/6656936">solution</a> but apparently it gives me errors. When I've tried it, the warning: </p>

<blockquote>
  <p>mysql_query() expects parameter 2 to be resource, object given in
  C:... on line ...</p>
</blockquote>

<p>and the problem </p>

<blockquote>
  <p>mysql_num_rows() expects parameter 1 to be resource, null given in
  C:... on line ...</p>
</blockquote>

<p>I am actually looking for a solution for this problem or a tutorial how to solve this problem.</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");
    }
?&gt;
&lt;?php
 ?&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 ($_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['submit'])) {
## connect mysql server
    $mysqli = new mysqli(localhost, root, "", loginsecure);
    # check connection
    if ($mysqli-&gt;connect_errno) {
        echo "&lt;p&gt;MySQL error no {$mysqli-&gt;connect_errno} : {$mysqli-&gt;connect_error}&lt;/p&gt;";
        exit();
    }
## query database
    # prepare data for insertion
    $username   = $_POST['username'];
    $mainpass = $_POST['password'];
    $password   = hash('sha256', $mainpass);
    $first_name = $_POST['first_name'];
    $last_name  = $_POST['last_name'];
    $email      = $_POST['email'];

    # check if username and email exist else insert
    // u = username, e = emai, ue = both username and email already exists
    $user = $_POST['username'];
  $usernamecheck=mysql_query("SELECT username FROM users WHERE username='$user'", $mysqli);
        if (mysql_num_rows($usernamecheck)&gt;=1){
    echo $user." is already taken";
 }
    else{
        # insert data into mysql database
        $sql = "INSERT  INTO `users` (`id`, `username`, `password`, `first_name`, `last_name`, `email`)
                VALUES (NULL, '{$username}', '{$password}', '{$first_name}', '{$last_name}', '{$email}')";

        if ($mysqli-&gt;query($sql)) {
            header("Location: checklogin.php?msg=Registered Successfully!");
        } else {
            echo "&lt;p&gt;MySQL error no {$mysqli-&gt;errno} : {$mysqli-&gt;error}&lt;/p&gt;";
            exit();
        }
    }
}
}
?&gt;
&lt;hr /&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>