I have WebGL games that are in a membership based PHP/MySQL site.
I would appreciate any code, resources etc that might point me in the right direction.
Questions:
#1. How do I collect info like MemberID, GameID (in MySQL), Game Started DateTime, Game Ended DateTime, Total Points earned etc.?
#2. How do I call PHP API to push the data mentioned in #1 above to the API?
Thanks in advance
I tested this once but i don’t know if that ist the best way… and i didn’t had to use it at the end.
Here are my examples, pearhaps it helps.
PHP (stringreader.php)
In this example i search for all items with the key NAME and the value “itemanme1” or “itemname2” or “itemname3”
<?php
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
//Database - fill it with your values...
$dbhost="localhost";
$dbuser="root";
$dbpasswort="12345";
$dbname="mydatabasename";
$table="mydatabasetablename";
//Search in this row...
$searchKey="name";
//Return this fields...
$findKey1="description";
$findKey2="price";
$conn = new mysqli($dbhost, $dbuser, $dbpasswort, $dbname);
if ($conn->connect_error)
{
echo "DB connection failed.";
die("Connection failed: " . $conn->connect_error);
}
else
{
//$_POST["str"] search for this string in your database
if (!empty($_POST["str"]))
{
//Split string by elements
$elemente = explode("¦", $_POST["str"]);
//sort array
asort($elemente);
foreach ($elemente as $element)
{
$sql = "SELECT id, $findKey1, $findKey2 FROM $table WHERE $searchKey='$element'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo $row["id"]. "|" . $row[$findKey1]. "|" .$element. "|" . $row[$findKey2] . "¦";
}
}
else
{
echo "0 results¦";
}
}
$conn->close();
}
else
{
//if the string transfer from unity had a error...
echo "Error: cannot get str";
}
}
?>
C# (collected some parts from my code…)
IEnumerator RequestString(string url = "stringreader.php")
{
string lookFor = "itemanme1¦itemname2¦itemname3";
if(lookFor != "")
{
WWWForm form = new WWWForm();
form.AddField("str", lookFor);
UnityWebRequest uwr = UnityWebRequest.Post(url, form);
uwr.chunkedTransfer = false; //workaround for unity 2017.3.0p1
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
DoSomethingWithTheString(true, uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
DoSomethingWithTheString(false, uwr.downloadHandler.text);
}
}
else
{
Debug.Log("No items found... " + lookFor);
}
}
private void DoSomethingWithTheString(bool error, string result)
{
//Remove the last split char between item results
if(result.Substring(result.Length - 1) == "¦")
{
result = result.Remove(result.Length - 1);
}
if(!error)
{
string[] Items = result.Split('¦');
}
}