drfu80954 2013-04-11 09:33
浏览 24
已采纳

从高分榜添加和显示新统计数据

Right now I've got my high score boardsaved as an SQL database which I'm accessing through Unity with some PHP scripts. My original score board allowed me to store a users name and their score, but I'm trying to add extra fields like accuracy etc. However, my new field isn't being displayed or passed to my server.

I have two PHP scripts called display.php and addscrore.php (spelling mistake I'll fix). Display.php looks like the following:

<?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'] . "
";
}
?>

Addscrore.php looks like this:

<?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()); 
        } 
?>

Then, in my Unity program I've made my HSController class with the following code:

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.
        }


     }
    }

Then, when I want to add some new information to my table I call the following line of code:

  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));
    }

However, with all of this, my new field, accuracy, still isn't being displayed. Can anyone see what I'm doing wrong and why my new field isn't being displayed when I call it?

  • 写回答

1条回答 默认 最新

  • dongque1646 2013-04-11 09:35
    关注

    May not be the cause at all, but in C# when you are building the post URL, you are missing an equals sign off "&accuracy"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题