简体   繁体   中英

Unable to get Data from MySQL using PHP

I have an online database on my server, where each username is assigned a primary key id. I want to get this id from the table with the help of PHP. The code is as follows: `

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

`

The PHP script works when I use it through my browser, where it prints the correct id number. However, when I call this PHP through Unity: `

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

` The code reaches the point "Connecting to PHP", but does not print anything after that in the console.

Can anyone help me out with this issue? Thanks

move

yield return wwwResponse; 

to the bottom of your code.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM