如果条件是真的但仍然跳跃在其他地方

I have created a login script using php and ajax. The password I echo the values and in return i found textbox value and database value to be same, but still its jumping to else statement. I am unable to figure out the problem with the code or what else I am doing wrong. May be one of you can see if anything wrong in my code

$username = $_POST['username'];
    $pass     = mysqli_real_escape_string($connection, $_POST['password']);
    $pass     = md5($pass);

    $check  = mysqli_query($connection, "SELECT * FROM users WHERE username = '$username'");
    $result = mysqli_num_rows($check);
    if($result == 1) {
        $data = mysqli_fetch_array($check);
        $password_check = $data[2]; 

        if($password_check == $pass) {
            $_SESSION['uid']  = $data[0];
            $_SESSION['name'] = $data[3];
            echo "Success";
        } else {
            echo "<div class='message'>Invalid Password</div>";
        }
    } else {
        echo "<div class='message'>Username/Password did not matched</div>";
    }