php - 无法使用 PHP 从 MySQL 获取数据

标签 php mysql sql unity-game-engine

我的服务器上有一个在线数据库,其中每个用户名都分配有一个主键 ID。我想在 PHP 的帮助下从表中获取这个 id。代码如下: `

<?php
   $servername = "uk9.siteground.eu";
   $username = "****";
   $password = "****";
   $dbname = "****";
   $tablename = "Account";
   $user = $_REQUEST["LoggedInUser"];
   // Create connection
   $conn = mysqli_connect($servername, $username, $password, $dbname);
   // Check connection
   if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
   }
   $sql = "SELECT id
           FROM $tablename
           WHERE 'username' LIKE '$user'";

   $result = $conn->query($sql)->fetch_object()->id;
   echo $result;
   mysqli_close($conn);
?>

`

当我通过浏览器使用 PHP 脚本时,它会工作,它会打印正确的 ID 号。但是,当我通过 Unity 调用此 PHP 时: `

public string username;
public string userID;

private void Start()
{
    username = "example";
    StartCoroutine(GetUID());
}

IEnumerator GetUID()
{
    string url = "****";
    WWWForm form = new WWWForm();
    form.AddField("LoggedInUser", username);
    WWW wwwResponse = new WWW(url, form);
    Debug.Log("Connecting to PHP");
    yield return wwwResponse;
    if (wwwResponse.error != null)
    Debug.Log(wwwResponse.error);
    else
    Debug.Log("No Error" + wwwResponse.text);
    userID = wwwResponse.text;
    Debug.Log(userID);
}

` 代码到达“连接到 PHP”点,但此后不会在控制台中打印任何内容。

谁能帮我解决这个问题吗?谢谢

最佳答案

移动

yield return wwwResponse; 

到代码底部。

示例:

IEnumerator GetUID() {

    string url = "****";
    WWWForm form = new WWWForm();
    form.AddField("LoggedInUser", username);
    WWW wwwResponse = new WWW(url, form);

    //wait till backend responses your request
    while(!wwwResponse.isDone) {
        yield return null; //skip frame
    }

    // keep going when you got your response 
    if(wwwResponse.error != null) {
        Debug.Log(wwwResponse.error);
    } else {
        Debug.Log("No Error" + wwwResponse.text);
        userID = wwwResponse.text;
        Debug.Log(userID);
    }

    yield return wwwResponse;
}

关于php - 无法使用 PHP 从 MySQL 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48824317/

相关文章:

mysql - SQL:空列计数不正确

php - 代码接收中的扫描输入

php - 使用PHP通过FTP上传文件

php - Laravel 队列反序列化问题

PHP 注册表单

MySQL 三路连接?

sql - select * from tablename 和 select column1, column2 from tablename 之间有性能差异吗?

php - 如何将 Laravel 中的文件直接上传到公用文件夹?

mysql - 找出 SQL 中的 AVG 列

sql - 如何进行 Sequelize 查询以获取一年中日期范围不可知的项目?