无法使用PHP从MySQL获取数据

时间:2018-02-16 10:06:46

标签: php mysql sql unity3d

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

package com.example.owner.foodal11_2;

/**
 * Created by Owner on 11/2/2018.
*/
import android.app.Application;
import android.text.TextUtils;  
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;


public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

        @Override
        public void onCreate() {
            super.onCreate();
            mInstance = this;
        }

        public static synchronized AppController getInstance() {
            return mInstance;
        }

        public RequestQueue getRequestQueue() {
            if (mRequestQueue == null) {
                mRequestQueue = Volley.newRequestQueue(getApplicationContext());
            }

            return mRequestQueue;
        }

        public <T> void addToRequestQueue(Request<T> req, String tag) {
            req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
            getRequestQueue().add(req);
        }

        public <T> void addToRequestQueue(Request<T> req) {
            req.setTag(TAG);
            getRequestQueue().add(req);
        }

        public void cancelPendingRequests(Object tag) {
            if (mRequestQueue != null) {
                mRequestQueue.cancelAll(tag);
            }
        }
    }

`

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

<?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);
?>

` 代码到达了点#34;连接到PHP&#34;,但在控制台之后不会打印任何内容。

任何人都可以帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:0)

移动

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;
}