将变量从C#发送到php for MySQL(Unity)

时间:2017-03-02 14:55:46

标签: c# php mysql sqlite unity3d

我是MySQL系统的新手。我必须存储在数据库中:

用户名; 得分了; 资源。

对于发送用户名,分数和资源我没有任何问题但是当我尝试从数据库获取资源时我有这个错误:

</style>
</head>

<body>
<h1>Bad request!</h1>
<p>


Your browser (or proxy) sent a request that
this server could not understand.

</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.

</p>

<h2>Error 400</h2>
<address>
<a href="/">localhost</a><br />
<span>Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1</span>
</address>
</body>
</html>


UnityEngine.Debug:Log(Object)
<SetPlayerName>c__Iterator1:MoveNext() (at       Assets/Scripts/DataBase/MySQL/DBLoader.cs:67)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

这是我的php文件:

<?php

    $servername = "host";
    $username    = "name";
    $password    = "";
    $dbname        = "game_name";

    $coinsname     = (isset($_REQUEST['coinsnameGet']) ? $_REQUEST['coinsnameGet'] : null);

    //Create Connection
    $connection = new mysqli($servername, $username, $password, $dbname);

    //Check Connection
    if(!$connection) {
        die("Connection Failed. " . mysqli_connect_error());
    }

    $sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
    $result = mysqli_query($connection, $sql);

    while($row = mysqli_fetch_array($result, MYSQLI_BOTH) {
        echo $row['Coins'];
    }
?>

我必须检查名称是否= =到本地数据库名称然后获取资源。 本地数据库是用sqlite创建的。

这是我的关联课程:

private DataBase dbLocal;

    WWW userData;
    WWW scoreData;
    WWW coinsData;

    public Text Coins;

    private string[] dbUsers;
    private string[] dbScores;

    private string dbCoins;

    private string postCoinsData = "http://host/game_name/UsercoinsData.php";

    public GameObject scorePrefab;
    public Transform scoreParent;

    public GameObject rankValue;
    public GameObject nameValue;
    public GameObject scoreValue;

    // Use this for initialization
    IEnumerator Start () {

        dbLocal = (DataBase)FindObjectOfType(typeof(DataBase));

        userData = new WWW("http://host/game_name/UsernameData.php");
        scoreData = new WWW("http://host/game_name/UserscoreData.php");

        yield return userData;
        yield return scoreData;

        string textUserData = userData.text;
        dbUsers = textUserData.Split(';');

        string textScoreData = scoreData.text;
        dbScores = textScoreData.Split(';');

        scoreParent.transform.localPosition = new Vector3(0, 0, 0);

        dbLocal.Connection();

        StartCoroutine(SetPlayerName());
        GenerateScore();
    }

    IEnumerator SetPlayerName()
    {
        string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());
        WWW dataCoins = new WWW(setName);

        Debug.Log(dbLocal.GetName());

        yield return dataCoins;

        string textDataCoins = dataCoins.text;
        Coins.text = textDataCoins;
        Debug.Log(Coins.text); //This log the error
    }

如果我改变了这个:

$sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";

用这个:

$sql = "SELECT Coins FROM score WHERE Name = 'PlayerName'";

在浏览器中,我看到了分数。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

你的php永远找不到$_REQUEST['coinsnameGet'],因为你正在创建这样的网址:

string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());

看起来像:

  

http://host/game_name/UsercoinsData.phpcoinsnameGet =玩家名称

但应该看起来像

  

http://host/game_name/UsercoinsData.php?coinsnameGet=PlayerName

不完全确定这是导致您的问题的原因还是复制粘贴错误,但如果是,则应将您的代码更改为:

string setName = postCoinsData + "?coinsnameGet=" + WWW.EscapeURL(dbLocal.GetName());