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");
}
}
}