Hello,
I would like to save data that I have fetched from the database (the query does not yet protect against SQL injection) in the ‘Card’ class so that I can access it later. The whole thing should later be visualized in a family tree-like structure.
First I get the data from the database if the username matches:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess');
//check connection
if (mysqli_connect_errno())
{
echo "Error #1"; // error code #1 = Connection failed!
http_response_code(400);
exit();
}
$username = $_POST["name"];
// $level = $_POST["level"];
$sql = "SELECT * FROM playercards WHERE nickname ='" . $username . "';"; //"'"' AND level ='" . $level . "';";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = $result->fetch_assoc()) {
echo $row ["CardID"] . "+" . $row ["CardName"] . "+" . $row ["score"] . "+" . $row ["nos"] . "+" . $row ["tmp"] . "+" . $row ["OfficerID"] . "+" . $row ["ParentID"];
}
}
else {
echo "No cards found.";
exit();
}
?>
For each result that is available, the query fetches a series of data that is delimited with a ‘+’. He then saves this in the Card class:
string[] DataStringArray = www1.downloadHandler.text.Split("+"); // Splits the data into an string array.
//All content needs to be deleted first to avoid duplicates.
//Fills the Card class with the data from the database.
Level61.CardID = int.Parse(level61DataStringArray[0]);
Level61.CardName = level61DataStringArray[1];
Level61.score = int.Parse(level61DataStringArray[2]);
Level61.nos = int.Parse(level61DataStringArray[3]);
Level61.tmp = int.Parse(level61DataStringArray[4]);
Level61.officerID = int.Parse(level61DataStringArray[5]);
Level61.parentID = int.Parse(level61DataStringArray[6]);
public class Card : MonoBehaviour
{
public static int CardID;
public static string CardName;
public static int score; // The score this card creates for the player.
public static int level ;
public static int nos = 0; // Actual number of soldiers;
public static int tmp = 0; // Target number of soldiers;
public static int officerID;
public static int parentID;
}
As you can see from the code, it only saves this for the ‘Level61’ map. This is because I previously had a separate query for each map level, but that was way too much.
But it actually worked, so it didn’t throw any errors or anything.
I would now like to change this so that for every database hit
automatically makes a new entry. So for every card
a new entry must be made in the card class.
How do I best implement this? Only the section with the string array would have to be changed I guess…
It would also be nice if you could tell me how to delete existing data. If the player requests all data again, e.g. because he restarted the app, the Card class should be empty so as not to create duplicates.
Kind regards
Tobias