ngplayground
ngplayground

Reputation: 21627

PHP MySQLi jQuery checking database value doesn't exist

I'm using the below PHP and jQuery script and I'm trying to check that the username put through doesn't exist in the database already.

PHP

        if(mysqli_query($con, "SELECT id FROM user WHERE user_name='$username'")){
            echo '1'; //If there is a  record match in the Database - Not Available
        } else {
            echo '0'; //No Record Found - Username is available 
        }
    }
?>

jQuery

$(document).ready(function(){
    $("fieldset input[name=username]").change(function(){
        var username = $("fieldset input[name=username]").val();//Get the value in the username textbox
        if(username.length > 3){
            $(".username_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
            $.ajax({
                type: "POST",  
                url: "/donald/settings/check-username.php",  //file name
                data: "username="+ username,  //data
                success: function(data){
                    console.log("server_response: "+data);
                    if(data == '0'){
                        $(".username_status").html('<font color="Green"> Available </font>');
                    } else  if(data == '1'){  
                        $(".username_status").html('<font color="red">Not Available </font>');
                    }
                } 
            }); 
        } else {
            $(".username_status").html('<font color="#cc0000">Username too short</font>');
        }
        return false;
    });
});

The result always comes through as 1

Upvotes: 0

Views: 2198

Answers (4)

alwaysLearn
alwaysLearn

Reputation: 6950

You should check mysqli_num_rows instead . mysqli_query always return true if query is executed

Try changing php code to

$resource = mysqli_query($con, "SELECT id FROM user WHERE user_name='$username'");
$rowCount = mysqli_num_rows($resource);  
 if($rowCount > 0){
        echo '1'; //If there is a  record match in the Database - Not Available
    } else {
        echo '0'; //No Record Found - Username is available 
    }
}

Upvotes: 1

Eugen Rieck
Eugen Rieck

Reputation: 65274

(mysqli_query($con, "SELECT id FROM user WHERE user_name='$username'")) will allways evaluate as true, if the query went OK. THis is not what you want.

Upvotes: -1

Vyktor
Vyktor

Reputation: 20997

If you take a look at mysqli_query() documentation:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

So with valid query it should never return FALSE. Therefore you probably want to do this, using mysqli_result:

$q = mysqli_query($con, "SELECT id FROM user WHERE user_name='$username'");
if( $q && $q->num_rows ){
    echo '1';
} else {
    echo '0';
}

Upvotes: 3

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

mysqli_query() returns true everytime you have a correct query, no matter its result. You need to use another mysqli_ function which will return rows i.e. mysqli_num_rows, _result or _fetch_array.

Ofcourse, each of them, has its own syntax, so you have to check it and use it properly in order to check if there's a result or not. i.e. if num_rows>0 or result($query, 0)

Upvotes: 1

Related Questions