使用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

duanchan5458
duanchan5458 我从服务器得到空回复?你知道原因吗?
3 年多之前 回复
doulao7998636570
doulao7998636570 对于if和elseif语句,PHP代码中缺少两个}。
6 年多之前 回复
drnzpd579935
drnzpd579935 在PHP部分中,您读取了表变量$_GET['table'],但您似乎没有在C#代码中设置它。
6 年多之前 回复
dongwuchu0314
dongwuchu0314 道歉,我不太清楚该做什么?有什么建议?
6 年多之前 回复
dsjmrpym220113739
dsjmrpym220113739 在您发布的示例代码中,您不发送或设置表GET变量
6 年多之前 回复
doupingtang9627
doupingtang9627 我正在为iPhone和Android应用程序开发它
6 年多之前 回复
dqppv86022
dqppv86022 您正在开发哪个平台?如果获胜,那么你可以尝试使用'System.Net.HttpWebRequest'
6 年多之前 回复

1个回答



让我尝试将其重写为一个工作示例:</ p>

C#</ h2> \ n
  void Start(){
StartCoroutine(GetData());
}

IEnumerator GetData(){
gameObject.guiText.text =“Loading ...” ;
WWW www = new WWW(“http://yoururl.com/yourphp.php?table=shoes”); // GET数据通过URL

发送(!www.isDone&amp;&amp; string.IsNullOrEmpty(www.error)){
gameObject.guiText.text =“Loading ...”+ www。 Progress.ToString( “0%”); //显示进度
yield return null;
}

if(string.IsNullOrEmpty(www.error))gameObject.guiText.text = www.text;
else Debug.LogWarning(www.error);

}
</ code> </ pre>

PHP </ h2>

 &lt;?php 

//数据库连接到此处\ n
if($ _REQUEST ['table'] ===“shoes”){//我正在使用REQUEST而不是GET,所以它可以同时使用GET和POST
$ query =“SELECT * FROMshoes订购'价格'ASC LIMIT 10“;
} elseif($ _REQUEST ['table'] ===”sneakers“){
$ query =”SELECT * FROMsports ORDER byprice ASC LIMIT 10“;
}

$ result = mysql_query($ query)或die(mysql_error());

而($ row = mysql_fetch_assoc($ result)){
echo $ row ['shopname' ]。 “\ t”。 $ row ['price']。 “
” 个;

}
?&gt;
</ code> </ pre>

希望这会有所帮助!</ p>
</ div>

展开原文

原文

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!

dongwen7371
dongwen7371 我从服务器得到空回复? 你知道原因吗?
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐