Search Unity

Fetching Mysql data with UnityWebRequest.Get is not working

Discussion in 'Scripting' started by lovemoebius, Apr 22, 2019.

  1. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    I am trying to fetch data from a database with UnityWebRequest.

    This is how I've set up my function inside of Unity:


    Code (CSharp):
    1.  
    2.     public string[] comments;
    3.     public string requestUrl = "http://creativiii.com/architect/data.php?action=retrieve&model_nameGet=test";
    4.     public IEnumerator Retrieve()
    5.     {
    6.  
    7.         using (UnityWebRequest webRequest = UnityWebRequest.Get(requestUrl))
    8.         {
    9.             // Request and wait for the desired page.
    10.             yield return webRequest.SendWebRequest();
    11.             if (webRequest.isNetworkError)
    12.             {
    13.                 Debug.Log("Error: " + webRequest.error);
    14.             }
    15.             else
    16.             {
    17.                 Debug.Log("Received: " + webRequest.downloadHandler.text);
    18.                 string fulldata = webRequest.downloadHandler.text;
    19.                 comments = fulldata.Split(new string[] { "<br>" }, StringSplitOptions.None);
    20.                 Debug.Log(String.Format("There are {0} comments.", comments.Length));
    21.                 foreach (string comment in comments)
    22.                 {
    23.                     Debug.Log(comment);
    24.                 }
    25.             }
    26.         }
    27.     }
    And this is how the data.php file is set up:

    Code (CSharp):
    1.  
    2.     // Create connection
    3.     $conn = new mysqli($servername, $username, $password, $dbname);
    4.     // Check connection
    5.     if ($conn->connect_error) {
    6.         die("Connection failed: " . $conn->connect_error);
    7.     }
    8.  
    9.     if ($action_type == "retrieve") {
    10.  
    11.         $sql = " SELECT * FROM `ar_comments` WHERE `model_name` = '".$model_name_get."'";
    12.         $result = $conn->query($sql);
    13.  
    14.         $push = "INSERT INTO `ar_comments`(`model_name`,`comment`,`position_x`,`position_y`,`position_z`) VALUES ('".$model_name."','".$comment."','".$position_x."','".$position_y."','".$position_z."')";
    15.  
    16.         if ($result->num_rows > 0) {
    17.             // output data of each row
    18.             while($row = $result->fetch_assoc()) {
    19.                 echo "Comment: " . $row["comment"]. " - Position: " . $row["position_x"]. ", " . $row["position_y"]. ", " . $row["position_z"]. "<br>";
    20.             }
    21.         } else {
    22.             echo "0 results";
    23.         }
    24.  
    25.     }
    If I visit the URL from the first script it will correctly return the data I have requested.
    However, If I request it from Unity, I will only receive:

    0 results


    Why is this happening? Is there something wrong with my C# code?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It seems your code is fine.

    Here was what I got directly from your URL when I ran your code (unchanged) in Unity 2018.2.10:

    Screen Shot 2019-04-22 at 12.24.55 PM.png
     
  3. lovemoebius

    lovemoebius

    Joined:
    Dec 12, 2016
    Posts:
    88
    I still get no results on my end.

    Is Unity or PHP caching the results somehow? How can I fix this?

    EDIT: I have tried forcing Mysql to not cache and UnityWebRequest.ClearCookieCache() but neither worked.
     
    Last edited: Apr 24, 2019
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Try printing webRequest.url after request, maybe the query got somehow corrupted.
     
  5. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
    I'm experiencing the same issue in version 2019.1.6
     
  6. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
    Interesting detail... it happens in PC but not in Mac
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Have you tested that url in browser on the same PC where you testing windows build? Ive checked and it works on my side both in unity and browser, I'm on windows now. Maybe the issue is coming from network configuration?