I try to work inside Unity scripts with JSON datas from a SQL table. I'm using PHP to communicate with Unity.
Here is my PHP script:
<?php
try
{
$bdd = new PDO('mysql:host=xxx.mysql.db;dbname=xxx;charset=utf8', 'xxx', 'xxx');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$Wallet_Number = $_GET['Wallet_Number'];
$req = $bdd->prepare('SELECT Wallet_Number, NFT, Montant_Mise, Date_Spin FROM Wallets_Bets WHERE Wallet_Number = ?');
$req->execute(array($_GET['Wallet_Number']));
$req->setFetchMode(PDO::FETCH_ASSOC);
$json['Wallet_Number'] = $req->fetchAll();
$json['content'] = count($json['Wallet_Number']);
echo json_encode($json);
?>
The script is working fine, here is a result from a request:
{"Wallet_Number":[{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0,010000","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"RetourFutur","Montant_Mise":"0,01","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"MenInBlack","Montant_Mise":"0,01","Date_Spin":"06\/04\/2022"},{"Wallet_Number":"XXX","NFT":"Shinning","Montant_Mise":"0,10","Date_Spin":"06\/04\/2022"}],"content":6}
Here is my Unity class to manage de datas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class OVHJsonObject
{
// Start is called before the first frame update
public string Wallet_Number;
public string NFT;
public string Montant_Mise;
public string Date_Spin;
}
And finaly the Unity script which manage the JSON datas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Newtonsoft.Json;
using SimpleJSON;
namespace AllArt.Solana.Example
{
public class NGetJSONData : MonoBehaviour
{
string queryURL = "https://xxx.fr/CheckWallet.php?Wallet_Number=";
public static string NPKey;
public string NValue;
public static OVHJsonObject[ ] Wallet_data = new OVHJsonObject[9] ;
void Start()
{
}
public void PopulateList() {
NPKey = SimpleWallet.instance.wallet.GetAccount(0).GetPublicKey;
Debug.Log (" NPKEY : " + NPKey);
StartCoroutine(GetRequest(queryURL + NPKey));
}
IEnumerator GetRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = uri.Split('/');
int page = pages.Length - 1;
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError(pages[page] + ": Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
NValue = webRequest.downloadHandler.text;
Wallet_data[0] = JsonUtility.FromJson<OVHJsonObject>(NValue);
Debug.Log(" Wallet Name : "+ Wallet_data[0] .Wallet_Number);
break;
}
}
}
// Update is called once per frame
void Update()
{
}
}
}
}
When running, I get no error from Unity but the fowllowing line always send an empty value (no null but nothing inside)
Debug.Log(" Wallet Name : "+ Wallet_data[0] .Wallet_Number);
Do you know what I am missing ? It seems the JSON format from my PHP script is not recognize in my Unity script.
Thank you,