使用php unity3d从数据库中检索多个值 [英] Retrieving multiple values from the database using php unity3d
问题描述
我正在制作类似于RPG游戏的游戏,用户可以登录并保存和加载数据。我可以登录并保存。但是,当我要加载数据时,我很难加载它。
I am making like RPG game where user can login and save and load the data. I can login and save. But, when I want to load the data, I am having a trouble to load that.
我只会发布必要的代码(PHP代码(Load.php)) :
I will post the necessary code only (PHP code (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 $Username;
echo $Location;
}
die();
}
else
{
die ("Data does not exist!");
}
?>
在这里,我在Unity中通过脚本访问它(问题所在的必需代码):
And here where I access it by script in Unity (necessary code where the problem is):
IEnumerator LoadCoroutine(WWW _www)
{
yield return _www;
if (_www.error == null)
{
message = _www.text;
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");
}
}
else
{
message = "Error while trying to connect to the server.\nMessage: " + _www.error;
clickedLoadGame = false;
}
}
脚本仅返回单个值,即基于PHP代码的$ Location ,例如,当我想检索 $ Username 时, _www.text 将是 VillageYunnan 这不是我想要的,我只希望将单个值打印在 _www.text 上,例如 Village 或 Yunnan ,而不是 > VillageYunnan ,并且仅用于2个值,如果我想在PHP中检索所有 $ ,则类似于 VillageYunnan15101010100 ,依此类推,
The script only return single value which is $Location based on PHP code, and when I want to retrieve the $Username for example, the _www.text will be VillageYunnan which is I dont want, I want only the single value will be printed on _www.text, like Village or Yunnan and not VillageYunnan and that is only for 2 values, if I want to retrieve all of the $ in PHP, it will be like VillageYunnan15101010100 and so on, which will be very hard to maintain.
我的问题是我只希望 _www.text 仅打印单个值,而不希望将其合并在一起我以前提到过。
My question is I only want the _www.text only print like a single value and not combined together like I mention before.
有帮助吗?
真的很感谢您的回答。
谢谢
编辑后:(必需的鳕鱼e)
GameManager.cs
public static void WebsiteConnect(string link)
{
wwwForm = new WWWForm();
wwwForm.AddField("Username", LoggedInUsername);
wwwForm.AddField("Gold", UserInformation.CurrentGold);
wwwForm.AddField("Level", Status.level);
wwwForm.AddField("Attack", Status.attack);
wwwForm.AddField("Defense", Status.defense);
wwwForm.AddField("MagicDefense", Status.magicDefense);
wwwForm.AddField("Evasion", Status.evasion);
wwwForm.AddField("Health", Status.hp);
wwwForm.AddField("MaxHealth", Status.maxHp);
wwwForm.AddField("Mana", Status.mp);
wwwForm.AddField("MaxMana", Status.maxMp);
wwwForm.AddField("Exp", Status.CurrentExperience);
wwwForm.AddField("NextExp", Status.NextLevelExperience);
wwwForm.AddField("AcceptedChiefQuest", QuestManager._acceptedChiefQuest);
wwwForm.AddField("AddedChiefQuest", QuestManager._addedChiefQuest);
}
www = new WWW(link, wwwForm);
}
我这样称呼它:
private void OnGUI()
{
if (GUILayout.Button("Continue", customStartButton.customStyles[0]))
{
clickedLoadGame = true;
audio.PlayOneShot(audioSEClip);
Load();
}
}
private void Load()
{
GameManager.WebsiteConnect("WEBSITE_ADDRESS/Load.php");
StartCoroutine(LoadCoroutine(GameManager.www));
}
IEnumerator LoadCoroutine(WWW _www)
{
yield return _www;
if (_www.error == null)
{
message = _www.text;
var jsonObject = JSON.Parse(_www.text);
//string jsonString = jsonObject.ToString();
Debug.Log(jsonObject);
if (_www.text == "Data does not exist!")
{
message = "Saved data does not match!";
mainMenu = false;
fadeAudio = false;
yield return new WaitForSeconds(3);
clickedLoadGame = false;
Reset();
}
else
{
if (_www.text == "Village")
{
message = "Successfully Loaded.";
GameManager.CurrentLocation = "Village";
GameManager.Loader = 1;
mainMenu = false;
fadeAudio = true;
yield return new WaitForSeconds(3);
Reset();
GameManager.LoadLevel("Loading Scene");
}
else if (_www.text == "Yein Plain")
{
message = "Successfully Loaded.";
GameManager.CurrentLocation = "Yein Plain";
GameManager.Loader = 4;
mainMenu = false;
fadeAudio = true;
yield return new WaitForSeconds(3);
Reset();
GameManager.LoadLevel("Loading Scene");
}
else
{
message = "Data does not exist!";
mainMenu = false;
fadeAudio = false;
yield return new WaitForSeconds(3);
clickedLoadGame = false;
Reset();
}
}
}
else
{
message = "Error while trying to connect to the server.\nMessage: " + _www.error;
clickedLoadGame = false;
}
}
这是PHP代码(Load.php) :
And here is the PHP code (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("WEBSITE_ADDRESS","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))
{
if ($Username == $row['Username'])
{
$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'];
json_encode($row);
}
else
{
die ("Saved data does not match!");
}
}
die();
}
else
{
die ("Data does not exist!");
}
?>
当我删除所有 $ Username
并等等,仅将其替换为 json_encode($ row)
,如果PHP代码仍然如上,结果仍然相同,则指向 string jsonString = jsonObject.ToString();
和 var jsonObject = JSON.Parse(_www.text);
为空。
When I delete all the $Username
and so on and replace it with json_encode($row)
only and if the PHP code stays like above, the result still same, there is an error pointed at string jsonString = jsonObject.ToString();
and the var jsonObject = JSON.Parse(_www.text);
is null.
推荐答案
您可以使用某些通用格式(例如xml或json)在不同应用程序之间共享结构化数据。
You can use some common format (like xml or json) to share structured data between different applications.
在PHP代码中替换:
$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 $Username;
echo $Location;
具有:
echo json_encode($row);
在C#代码中使用一些JSON库(例如 SimpleJSON )来解析JSON字符串并检索编码后的数据。
In C# code use some JSON library (like SimpleJSON) to parse JSON string and retreive encoded data.
这篇关于使用php unity3d从数据库中检索多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!