0
現在,我已經將我的高分評分板作爲一個SQL數據庫,我可以通過Unity通過一些PHP腳本訪問該數據庫。我的原始評分板允許我存儲用戶名和他們的分數,但我試圖添加額外的字段,如準確性等。但是,我的新字段沒有顯示或傳遞到我的服務器。添加並顯示來自高分計劃板的新統計信息
我有兩個名爲display.php和addscrore.php的PHP腳本(我會修正拼寫錯誤)。看起來Display.php的類似如下:
<?php
// Send variables for the MySQL database class.
$database = mysql_connect('localhost', 'dark', 'dark') or die('Could not connect: ' . mysql_error());
mysql_select_db('dark') or die('Could not select database');
$query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 5";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $row['name'] . "\t" . $row['score'] . "\t" . $row['accuracy'] . "\n";
}
?>
Addscrore.php看起來是這樣的:
<?php
$db = mysql_connect('localhost', 'dark', 'dark') or die('Could not connect: ' . mysql_error());
mysql_select_db('dark') or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET['name'], $db);
$score = mysql_real_escape_string($_GET['score'], $db);
$accuracy = mysql_real_escape_string($_GET['accuracy'], $db);
$hash = $_GET['hash'];
$secretKey="mySecretKey"; # Change this value to match the value stored in the client javascript below
$real_hash = md5($name . $score . $accuracy . $secretKey);
if($real_hash == $hash)
{
// Send variables for the MySQL database class.
$query = "insert into scores values (NULL, '$name', '$score', '$accuracy');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>
然後,在我的團結節目,我做了我的HSController類用下面的代碼:
using UnityEngine;
using System.Collections;
public class HSController : MonoBehaviour
{
private string secretKey = "mySecretKey"; // Edit this value and make sure it's the same as the one stored on the server
public string addScoreURL = "/score/addscrore.php?"; //be sure to add a ? to your url
public string highscoreURL = "highscoretable/display.php";
void Start()
{
StartCoroutine(GetScores());
}
// remember to use StartCoroutine when calling this function!
public IEnumerator PostScores(string name, int score, float accuracy)
{
//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
string hash = MD5.Md5Sum(name + score + accuracy + secretKey);
string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&accuracy" + accuracy + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
}
// Get the scores from the MySQL DB to display in a GUIText.
// remember to use StartCoroutine when calling this function!
public IEnumerator GetScores()
{
gameObject.guiText.text = "Loading Scores";
WWW hs_get = new WWW(highscoreURL);
yield return hs_get;
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
}
然後,當我想向我的表中添加一些新的信息時,我會調用以下代碼行:
string name = "Rawr";
int score = 1325;
float accuracy = 40.0f;
HSController _test;
// Use this for initialization
void Start()
{
_test = new HSController();
StartCoroutine(_test.PostScores(name, score,accuracy));
}
但是,所有這些,我的新領域,準確性,仍然沒有被顯示。任何人都可以看到我做錯了什麼,以及爲什麼當我打電話時,我的新字段沒有顯示?