Error from WWW returning Null

I am testing out getting data from MYSQL using PHP and JSON. The $email variable actually has an email not in my database, so it should return invalid email or password, which it does when I run the PHP script alone. However, when my unity script is compiled, it returns:

Debug.Log(w.text) → {“success”:false,“error”:“invalid email or password.”}
Debug.Log(w.error) → Null
Debug.Log(“Success”) → Success

I can’t figure out why it’s seeing the w.error as null in unity. Any Help?

PHP File:

<?php

    include 'connect_to_database.php';
   
    $email = "teest@email.com";
    $password = "password";
   
    $statementHandle = $databaseHandle->query( "SELECT userEmail FROM users WHERE userEmail='$email' and userPass='$password' ");
    $statementHandle->setFetchMode(PDO::FETCH_ASSOC);
   
    $result = $statementHandle->fetchAll();
   
    if($result){
        //echo 'Login successful.';
        $dataArray = array('success' => true, 'error' => "");
    }else{
        //echo 'Incorrect email or password.';
        $dataArray = array('success' => false, 'error' => "invalid email or password.");
    }
   
    header('Content-Type: application/json');
    echo json_encode($dataArray);

?>

And my C# code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Login : MonoBehaviour {

    string PHP_LoginUrl = "http://localhost/action_login.php";

    WWWForm form;
   
   public void LoginButtonPressed()
    {
        StartCoroutine("RequestLogin");
    }

    public IEnumerator RequestLogin() {

        /*
        form = new WWWForm();
        form.AddField("email", testEmail);
        form.AddField("password", testPassword);
        */

        WWW w = new WWW(PHP_LoginUrl);
        yield return w;

        if (string.IsNullOrEmpty(w.error)){
            Debug.Log(w.text);
            Debug.Log(w.error);
            Debug.Log("Success");
        }
        else
        {
            Debug.Log("Invalid email or password");
        }
    }
}

I’m not that certain of an answer, but you might look into UnityWebRequest instead Unity - Scripting API: UnityWebRequest

Which may handle errors differently. Just a thought if nobody can give you a better answer.

The page successfully downloaded, so w.error is null. If the page failed to download for whatever reason, like going to the wrong address, you would get a non-null result. You need to parse the contents of w.text to determine if your database request was successful or not.

WWW.error will be non-empty only when it can’t communicate with the server. UnityWebRequest is more flexible in this case as it will also report HTTP errors (4xx and 5xx responses).
Looking at your php code there seems to be no error from HTTP communication perspective, just a normal exchange of data.