Having trouble trying to verify a string returned from a UnityWebRequest.POST

Im not new to coding but I am new to Unity. I am simply setting up a sample project to store and retrieve data from a MySQL database. At the moment I has MAMP setup locally and a local MySql database setup through MAMP. That all works. I can query against and insert data.

What I cant figure out it why I cannot do a simple string varification on the text data returned from a UnityWebRequest.post. Im simply checkign to see if the string data returned from the php filevia aUNityWebRequest.post is equal to “nakGood.” The debug.log indeed shows it is “nakGood” but my if statement always returns false.

From my local php script running in MAMP, I simply query the table to see if a user is already registered. If not I add a new record. So I want to pass back to my Unity script whether the registration was successful or not.

It all works until I try to check the results of the www.downloadHandler.text

My simple PHP code is this:

So if its successful im returning back the string “nakGood” or even “0” (same results)

My Unity script is this: [the line not working as I think it should below is if(nakDataBack.Equals(“nakGood”))]

IEnumerator Register()
{
Debug.Log(“Register started attempt”);
WWWForm nakForm = new WWWForm();
nakForm.AddField(“nakname”, nameField.text) ;
nakForm.AddField(“nakpassword”, passwordField.text);

using (UnityWebRequest www = UnityWebRequest.Post(“http://localhost/sqlconnectsample/nakregister.php”, nakForm))
{

www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();

Debug.Log(www.downloadHandler.text);

string nakDataBack = www.downloadHandler.text;
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(“1:” + www.result + “<<>>” + www.responseCode) ;
}
else
{
if(nakDataBack.Equals(“nakGood”))
{
Debug.Log(“User created successfully”);
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
else
{
Debug.Log("register call returned: " + nakDataBack);
}

}

}

}

So you can see from the below screenshot what is returned in my DEbug.Log is:
“register call returned: nakGood”
BUT what should be returned in the Debug.Log is User created successfully when successful!
You can see from the Debug.Log that www.downloadHandler.text returns “nakGood”
See the below image for proof.

194948-upload-2022-4-10-15-2-31.png

Im new to UNITY, I’m sure its something easy that I dont understand BUT what I dont get is how this if statement is not returning as TRUE when indeed www.downloadHandler.text does equal “nakGood”?

if(nakDataBack.Equals(“nakGood”))
This is the above line of code not working as I think it should.
Of course I have tried if(nakDataBack == “nakGood”) as well as if(www.downloadHandler.text == “nakGood”) and it always returns FALSE

I need to know what the result was and I need to be able to check the result of running my PHP script from Unity to verify what occurred, what the actual result it.

As you can see in the screen shot www.downloadHandler.text is retuning “nakGood” (see the second Debug.Log output) so why in the world can’t I do a simple string comparison in an if statement and take the appropriate action. It always returns false and runs the else statement.

Any ideas?
Thanks
nate

As Bonfire already said in the comment you most likely have additional characters at the end of your text. Keep in mind that php files are returned “as they are” just all <?php .... ?> are stripped out . So if there are empty lines or even spaces behind the closing angle bracket, it would be returned when you request this php file.