How Do i check for Duplicate username in database

Hi I am trying to implement a simple login system. I am using the WWW method and WWW.Form.

//Create a form
var form = new WWWForm();

//Put all form data in a variable
var rawData =;
var url = "http://url/login.php";

var w = WWW(url,rawData);
yield w;
  print("There was an error logging in: " + w.error);

The problem is when i type in a duplicate username, mysql throws up an error as it should

Error :Duplicate entry 'UnityUser' for key 'PRIMARY'

But the w.error() does not resolve as true ( I am guessing since there is no HTML or PHP error but rahter a MySQL error)

y question is how do i stop the users from using duplicate usernames ?

You can make your php code return a certain value if there is a mysql error.

You can do this by using the php function named echo.

For example, you could return/echo 1 if a mysql error occurs.

(You might return/echo zero to represent success)

You can then check the return code in Unity by inspecting w.text, which will represent the content of the requested web page.

When you call echo in your php script, you're writing content to a web page.

Each HTTP request returns a status code. If everything goes well, your HTTP server returns the code “200 - OK”.

Your PHP script returns 200 by default, unless you explicitly tell it otherwise. Make sure you set the correct response header. For server side errors, a code in the 5xx range is common, for user errors, a 4xx code is used.

I would return 400 (User error) to indicate that the parameters passed to the server were invalid.

Read the PHP manual on setting server-side response codes.

The status codes can be found in the HTTP-standard, or more conveniently here.

MySql will throw an error only when you try to store value in the database. If the username name is unique in database, sql will throw an error. In your code it seems like you are trying to log in and not tryin to register. In case of login you should not get a duplicate entry error. There has to be something wrong on your PHP code. Check if you are doing an INSERT query or SELECT query on the PHP side.

This is my PHP file for double user verification:

    $sqlCheckUsername="SELECT 1 FROM Users WHERE Username = '" . mysql_real_escape_string($insertedUsername) . "'";
    $sqlCheckEmail="SELECT 1 FROM Users WHERE Email = '" . mysql_real_escape_string($insertedEmail) . "'";
    $queryUsername=mysql_query($sqlCheckUsername,$con) or die('error');
    $queryEmail=mysql_query($sqlCheckEmail,$con) or die('error');
    $Username = mysql_fetch_assoc($queryUsername);
        echo "Username already exists, choose a different one";
        echo "E-Mail already registered";
        echo "true";

It basically checks to see if there already is a row with that username or email in it.
I’m using OR, or rather, both controls, so people will know what they should change. Just like when your username OR password are wrong, that way you don’t have any doubts for where you’re wrong.