WWW以json格式从php mysql接收C#中的数据

问题描述 投票:0回答:2

我试图从mamp上托管的mysql数据库中读取数据,以在统一场景中显示玩家信息。我可以读写数据库,但我不知道如何以统一的方式显示信息。我读到你可以使用json格式化数据,然后统一显示,但我不知道从哪里开始。任何帮助将不胜感激。

using System.Collections; 
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetPasswords : MonoBehaviour
{
string URL = "http://localhost:8888/sqlconnect/usergetpasswords.php";
public string[] usersData;

public void RetrievePasswords()
{
    StartCoroutine(GetPasswords());
}

IEnumerator DisplayPasswords()
{
    WWWForm form = new WWWForm();
    form.AddField("email", DBManager.email);
    WWW www = new WWW(URL);
    yield return www;
    string usersDataString = www.text;
    usersData = usersDataString.Split(';');
    Debug.Log(usersData);
}
// Update is called once per frame
void Start()
{
    RetrievePasswords();
}

}

PHP脚本

<?php

//place the relative position of the database such as https://www.000webhost.com/cpanel-login?from=panel.
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); //replace root, root with your server username and password.


//Check that connection happened.
if (mysqli_connect_error())
{
//echo similar to debug, the message will populate the www.    
    echo "1: Connection failed"; //error code #1 = connection failed
    exit();
}

$email = $_POST["email"];

$sql = "SELECT email, account1, password1 FROM players WHERE email='" .$email . "';";


if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo ("email: ".$row['email']."|account1: ".$row['account1']."|password1: ".$row['password1'].";");
    }
}

?>

c# php mysql unity3d mamp
2个回答
1
投票
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetPasswords : MonoBehaviour
{

    string URL = "http://localhost:8888/sqlconnect/usergetpasswords.php";

    public void RetrievePasswords()
    {
        StartCoroutine(DisplayPasswords());
    }

    IEnumerator DisplayPasswords()
    {
        WWWForm form = new WWWForm();
        form.AddField("email", DBManager.email);
        WWW www = new WWW(URL, form);
        yield return www;
        Debug.Log("User Info = " + www.text);
    }

}

PHP脚本

<?php

    //place the relative position of the database such as https://www.000webhost.com/cpanel-login?from=panel.
    $con = mysqli_connect('localhost', 'root', 'root', 'unityaccess'); //replace root, root with your server username and password.


    //Check that connection happened.
    if (mysqli_connect_error())
    {
    //echo similar to debug, the message will populate the www.    
        echo "1: Connection failed"; //error code #1 = connection failed
        exit();
    }

    $email = $_POST["email"];

    $sql = "SELECT email, account1, password1 FROM players WHERE email='" .$email . "';";
    $result = mysqli_query($con, $sql);


    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo ("email: ".$row['email']."|account1: ".$row['account1']."|password1: ".$row['password1'].";");
        }
    }

?>

0
投票

你不应该再使用WWW类,因为它已经过时了https://docs.unity3d.com/ScriptReference/WWW.html

您应该使用UnityWebRequest而不是https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html

您还应该能够使用IMultipartFormSection或byte []替换WWWForm

现在我没有任何代码,但我可以在3小时内给你一个例子。

你仍然可以在这里得到一个例子:How to upload/download a file with Microsoft graph API in Unity3d

请注意,我确定将删除WWW类,因为我联系了Unity员工以获取替换它的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.