I am trying to read data from a mysql database hosted on mamp to display a players information in a unity scene. I can read and write to the database but I don’t know how to display the information in unity at all. I read that you can use a json to format the data and then display that in unity but I don’t know where to begin. Any help would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GetPasswords : MonoBehaviour
{
string URL = "http://localhost:8888/sqlconnect/usergetpasswords.php";
public string[] usersData;
public void RetrievePasswords()
{
StartCoroutine(GetPasswords());
}
IEnumerator DisplayPasswords()
{
WWWForm form = new WWWForm();
form.AddField("email", DBManager.email);
WWW www = new WWW(URL);
yield return www;
string usersDataString = www.text;
usersData = usersDataString.Split(';');
Debug.Log(usersData);
}
// Update is called once per frame
void Start()
{
RetrievePasswords();
}
}
php script
<?php
//place the relative position of the database such as https://www.000webhost.com/cpanel-login?from=panel.
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); //replace root, root with your server username and password.
//Check that connection happened.
if (mysqli_connect_error())
{
//echo similar to debug, the message will populate the www.
echo "1: Connection failed"; //error code #1 = connection failed
exit();
}
$email = $_POST["email"];
$sql = "SELECT email, account1, password1 FROM players WHERE email='" .$email . "';";
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo ("email: ".$row['email']."|account1: ".$row['account1']."|password1: ".$row['password1'].";");
}
}
?>
2
Answers
php script
You should not use WWW class anymore since it is obsolete
https://docs.unity3d.com/ScriptReference/WWW.html
You should use UnityWebRequest instead
https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html
You should also be able to replace WWWForm with IMultipartFormSection or byte[]
Right now I don’t have any code with me, but I will be able to give you an example in 3 hours.
Still you can get an example right here:
How to upload/download a file with Microsoft graph API in Unity3d
Note that I’m sure WWW class will be removed since I contacted Unity staff for the solution to replace it.