0

I want to check if the username exists in my database. I wrote the following code to check if the username exists but it isn't doing anything. I also don't know how to code in java that's why I put it in my php file. Can someone help me?

$statement = mysqli_prepare($con, "INSERT INTO users (username, password) VALUES (?, ?)");

if($username > 0){
    echo "This username is in use.";
}

mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

print_r(json_encode($response));
Charlie Fish
  • 13,172
  • 14
  • 64
  • 137
  • there are some line of code missing. You never set/check `$username`. – Jeff Jul 19 '16 at 23:48
  • 2
    You don't need to check if a username is already present when registering a new user. Simply put a UNIQUE constraint on the username column and catch any errors that MySQL throws at you when you attempt add a new row. – Dave Chen Jul 19 '16 at 23:49
  • You have two options: if username is set to be unique you can do an insert if not exists kind of thing (check this out: http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql). Or just do a select for that username before and check for rowcount. – Jeff Jul 19 '16 at 23:51

0 Answers0