Calling php script using C# (Unity)

前端 未结 1 1591
走了就别回头了
走了就别回头了 2021-01-16 22:03

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 t

相关标签:
1条回答
  • 2021-01-16 22:25

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

    Hope this helps!

    0 讨论(0)
提交回复
热议问题