Loading player position from database bug

First of all, I am not sure this is the right place to post this topic sorry about that.
I am using XAMPP and MYSQL to create my database everything works well but when I close the game and try to load the last position the player just teleports to the origin.

C# Code on unity:

Dataloader script

 using UnityEngine;
using System.Collections;

public class DataLoader : MonoBehaviour {
    public int playerxd;
    public int playeryd;
    public int playerzd;
    public string[] pos;
    IEnumerator Start(){

        WWW posData = new WWW("http://localhost/BegoneBD/DataPos.php");
        yield return posData;
        string posDataString = posData.text;
        print (posDataString);
        pos = posDataString.Split(';');
        print(GetDataValue(pos[0], "x:"));
        print(GetDataValue(pos[0], "y:"));
        print(GetDataValue(pos[0], "z:"));
    }

    string GetDataValue(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerxd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue2(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playeryd = System.Convert.ToInt32(value);
        return value;
    }
    string GetDataValue3(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        playerzd = System.Convert.ToInt32(value);
        return value;
    }

}

Save Script

public class Save : MonoBehaviour {
    public string[] pos;
    bool teste;
    float playerx;
    float playery;
    float playerz;
    int inputplayerx;
    int inputplayery;
    int inputplayerz;
    string CreateUserUrl = "http://localhost/BegoneBD/InsertPos.php";
    public void SavePosition()
    {
        playerx = transform.position.x;
        playery = transform.position.y;
        playerz = transform.position.z;
        inputplayerx = (int)playerx;
        inputplayery = (int)playery;
        inputplayerz = (int)playerz;
        CreatePos(inputplayerx, inputplayery, inputplayerz);
    }
    public void CreatePos(int playerx1, int playery1, int playerz1)
    {
        WWWForm form = new WWWForm();
        form.AddField("playerxpost", playerx1);
        form.AddField("playerypost", playery1);
        form.AddField("playerzpost", playerz1);
        WWW www = new WWW(CreateUserUrl, form);
    }
    public void LoadPosition()
    {
        DataLoader f = new DataLoader();
        f.playerxd = (int)playerx;
        f.playeryd = (int)playery;
        f.playerzd = (int)playerz;
        transform.position = new Vector3(playerx, playery, playerz);
    }
    public void CursorSaveOn()
    {
            Cursor.visible = true;

    }
    public void CursorSaveOff()
    {
        Cursor.visible = false;

    }
}

PHP Code for connection with the database:

Datapos.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "begonebd";

//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}

$sql = "SELECT id, x, y, z FROM playerposition";
$result = mysqli_query($conn ,$sql);

if(mysqli_num_rows($result) > 0){
//show data for each row
while($row = mysqli_fetch_assoc($result)){
echo "id:".$row['id'] . "|x:".$row['x']. "|y:".$row['y']. "|z:".$row['z'] . "
";
}
}-

Insertpos.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "begonebd";

$x = $_POST["playerxpost"];
$y = $_POST["playerypost"];
$z = $_POST["playerzpost"];
Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
else echo("Connection Success");

$sql = "SELECT id, x, y, z FROM playerposition";
$result = mysqli_query($conn ,$sql);

$sql = "INSERT INTO playerposition (x, y, z)
VALUES ('".$x."','".$y."','".$z."')";
$result = mysqli_query($conn ,$sql);

if(!result) echo "there was an error";
else echo "Everything ok.";-

Thanks in advance, this is really important for me since this is a school project and I cant do the presentation without the database working properly

Here are some things you can try:

  • Debug.Log the position received from database to confirm integrity. If the values are invalid then you need to review that part.
  • The code “transform.position = new Vector3(playerx, playery, playerz);” is in the class Save, is that class a component of the object Player? If not then it is moving the object from save and not the player, use the reference for player object instead if so.
  • Make note of the actual xyz value for player position before sending to the server, then look at it on the database, make sure they are relatively similar. Check the value received from DB in the game and make sure they are all at least similar. As the position in-game is used as float, the conversion to int could alter completely depending on the global scale used.

Let us know the results with more details if possible.

When do you run your save script? Does your db value return the right pos on next start?

I run the scrip save when the user clicks a button.
How can I check the return values from the database on the next start?

  1. How do I do that?
  2. It is a component of the object player.
  3. The values on the game and on the database are the same, but I dont know how can I check the values received from the DB

So… the script also works without the db at all…