使用php unity3d从数据库中检索单个值

时间:2015-03-11 06:11:11

标签: c# php unity3d

我正在制作RPG游戏,用户可以登录并保存和加载数据。我可以登录并保存。但是,当我想加载数据时,我很难加载它。

我将仅发布必要的代码(PHP代码(Load.php)):

<?PHP

$Username = $_POST['Username'];
$Location = $_POST['Location'];
$Gold = $_POST['Gold'];
$Level = $_POST['Level'];
$Attack = $_POST['Attack'];
$Defense = $_POST['Defense'];
$MagicDefense = $_POST['MagicDefense'];
$Evasion = $_POST['Evasion'];
$Health = $_POST['Health'];
$MaxHealth = $_POST['MaxHealth'];
$Mana = $_POST['Mana'];
$MaxMana = $_POST['MaxMana'];
$Exp = $_POST['Exp'];
$NextExp = $_POST['NextExp'];
$AcceptedChiefQuest = $_POST['AcceptedChiefQuest'];
$AddedChiefQuest = $_POST['AddedChiefQuest'];

$con = mysql_connect("SERVER_NAME","DATABASE_NAME","PASSWORD") or ("Cannot connect!"  . mysql_error());

if (!$con)
    die('Could not connect: ' . mysql_error());

mysql_select_db("DATABASE_NAME" , $con) or die ("Could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM Information WHERE `Username` = '".$Username."'");

$numrows = mysql_num_rows($check);

if ($numrows > 0)
{
    while($row = mysql_fetch_assoc($check))
    {
        $Username = $row['Username'];
        $Location = $row['Location'];
        $Gold = $row['Gold'];
        $Level = $row['Level'];
        $Attack = $row['Attack'];
        $Defense = $row['Defense'];
        $MagicDefense = $row['MagicDefense'];
        $Evasion = $row['Evasion'];
        $Health = $row['Health'];
        $MaxHealth = $row['MaxHealth'];
        $Mana = $row['Mana'];
        $MaxMana = $row['MaxMana'];
        $Exp = $row['Exp'];
        $NextExp = $row['NextExp'];
        $AcceptedChiefQuest = $row['AcceptedChiefQuest'];
        $AddedChiefQuest = $row['AddedChiefQuest'];

        echo $Location;
    }

        die("Successfully Loaded!");
}

else
{
    die ("Data does not exist!");
}


?>

在这里我通过Unity中的脚本访问它(问题所在的必要代码):

IEnumerator LoadCoroutine(WWW _www)
    {
        yield return _www;

        if (_www.error == null)
        {
            message = _www.text;

            if (_www.text == "Successfully Loaded!")
            {
                // Below code from this line, never gets executed (if and else if statement)
                // The `GameManager.CurrentLocation` appear as default value

                if (_www.text == "Village")
                {
                    GameManager.CurrentLocation = "Village";

                    GameManager.Loader = 1;
                }

                else if (_www.text == "Yein Plain")
                {
                    GameManager.CurrentLocation = "Yein Plain";

                    GameManager.Loader = 4;
                }

                // Below code from this line will gets executed.

                message = "Successfully Loaded.";

                mainMenu = false;

                yield return new WaitForSeconds(3);

                Reset();

                GameManager.LoadLevel("Loading Scene");
            }

            else if (_www.text == "Data does not exist!")
            {
                message = "Data does not exist";

                clickedLoadGame = false;
            }

            else if (_www.text == "Saved data does not match!")
            {
                message = "Saved data does not match";

                clickedLoadGame = false;
            }
        }

        else
        {
            message = "Error while trying to connect to the server.\nMessage: " + _www.error;

            clickedLoadGame = false;
        }
    }

当我Debug.Log message时,它看起来像这样:VillageSuccessfullyLoaded!,它永远不会显示为VillageYein Plain。似乎PHP代码中的echodie组合在一起。

我的问题是,如何从php而不是其他代码中检索$Location?因此,它只返回VillageYein Plain

真的很感激你的回答!

非常感谢

1 个答案:

答案 0 :(得分:1)

更改

die("Successfully Loaded!");

die();

然后:

        if (_www.text == "Data does not exist!")
        {
            message = "Data does not exist";

            clickedLoadGame = false;
        }

        else if (_www.text == "Saved data does not match!")
        {
            message = "Saved data does not match";

            clickedLoadGame = false;
        }

        else
        {
            if (_www.text == "Village")
            {
                GameManager.CurrentLocation = "Village";

                GameManager.Loader = 1;
            }

            else if (_www.text == "Yein Plain")
            {
                GameManager.CurrentLocation = "Yein Plain";

                GameManager.Loader = 4;
            }

            message = "Successfully Loaded.";

            mainMenu = false;

            yield return new WaitForSeconds(3);

            Reset();

            GameManager.LoadLevel("Loading Scene");
        }