douting0585 2013-12-16 00:45
浏览 67

使用C#调用php脚本(Unity)

I'm fairly new to both Unity and PHP, and I am currently working on a project where I can parse data from a MySQL database to Unity, using PHP.

I initially wanted to try and enable a method where the user can perhaps change the php script and enable it to choose a different table of data, however I was advised that it may be safer to list all variables within the php script and call it from Unity accordingly;

Display.php

$table = mysql_real_escape_string($_GET['table'], $db);

if ($table == "shoes") { 
    $query = "SELECT * FROM `shoes` ORDER by `price` ASC LIMIT 10";

elseif ($table == "sneakers") { 
    $query = "SELECT * FROM `sneakers` ORDER by `price` ASC LIMIT 10";

$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['shopname'] . "\t" . $row['price'] . "
";
}

I'm having trouble calling the php and choosing the table that I want to select, I am pretty new to this, so I apologise if this seems completely incompetent to you guys.

Here is the my Unity Script;

HSController.cs

void Start()
{
    StartCoroutine(GetScores());
}

// remember to use StartCoroutine when calling this function!
IEnumerator PostScores(string name, int score)
{

    string hash = Md5Sum(name + score + secretKey);

    string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;

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

IEnumerator GetScores()
{
    gameObject.guiText.text = "Loading...";
    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.
    }
}

Any help or a point in the right direction would be great!

Kind Regards

  • 写回答

1条回答 默认 最新

  • dotn30471 2014-08-08 10:04
    关注

    Let me try to rewrite this into a working example:

    C#

    void Start() {
        StartCoroutine(GetData());
    }
    
    
    IEnumerator GetData() {
        gameObject.guiText.text = "Loading...";
        WWW www = new WWW("http://yoururl.com/yourphp.php?table=shoes"); //GET data is sent via the URL
    
        while(!www.isDone && string.IsNullOrEmpty(www.error)) {
            gameObject.guiText.text = "Loading... " + www.Progress.ToString("0%"); //Show progress
            yield return null;
        }
    
        if(string.IsNullOrEmpty(www.error)) gameObject.guiText.text = www.text;
        else Debug.LogWarning(www.error);
    }
    

    PHP

    <?php
    
    //DB connection goes here
    
    if ($_REQUEST['table'] === "shoes") { //I'm using REQUEST instead of GET, so it will work with both GET and POST
        $query = "SELECT * FROM `shoes` ORDER by `price` ASC LIMIT 10";
    } elseif ($_REQUEST['table'] === "sneakers") { 
        $query = "SELECT * FROM `sneakers` ORDER by `price` ASC LIMIT 10";
    }
    
    $result = mysql_query($query) or die(mysql_error());
    
    while ($row = mysql_fetch_assoc($result)) {
        echo  $row['shopname'] . "\t" . $row['price'] . "
    "; 
    }
    ?>
    

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况