Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WebGL and MySQL

Discussion in 'WebGL' started by 8Observer8, Dec 28, 2015.

  1. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
    Hello,

    I can get access to MySQL from PC application. But when I build in WebGL and upload on my website I cannot send SQL query to MySQL

    I put this file on root of my website:

    crossdomain.xml

    Code (CSharp):
    1.  
    2. <?xml version="1.0" encoding="ASCII"?>
    3. <cross-domain-policy>
    4. <allow-access-from domain="*" secure="false"/>
    5. </cross-domain-policy>
    Thank you for your replay in advance
     
  2. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
    I will get you more information.

    My application works when I run from Unity Editor very well.

    I receive: Ok, we get: Hello

    But when I upload WebGL application on Google Drive Host it doesn't work. I see empty field.

    This all my code:

    Unity side:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UIManager : MonoBehaviour
    6. {
    7.     public InputField output;
    8.  
    9.     public void SaveStrOnClick()
    10.     {
    11.         StartCoroutine("SaveStr");
    12.     }
    13.  
    14.     IEnumerator SaveStr()
    15.     {
    16.         WWWForm form = new WWWForm();
    17.         form.AddField("str", "Hello");
    18.         WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
    19.         yield return www;
    20.         output.text = www.text;
    21.     }
    22. }
    Server side:

    Code (CSharp):
    1. <?php
    2.  
    3. if (!empty($_POST["str"]))
    4. {
    5.     $str = $_POST["str"];
    6.     echo "Ok, we get: ".$str;
    7. }
    8. else
    9. {
    10.    echo "Error: cannot get str";
    11. }
     
  3. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    584
    8Observer8 likes this.
  4. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
    Thanks!

    Is it right? I tried it but it doesn't work. I receive nothing:


    Code (CSharp):
    1. <?php
    2.  
    3. header("Access-Control-Allow-Credentials: true");
    4. header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
    5. header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    6. header('Access-Control-Allow-Origin: *');
    7.  
    8. if (!empty($_POST["str"]))
    9. {
    10.     $str = $_POST["str"];
    11.     echo "Ok, we get: ".$str;
    12. }
    13. else
    14. {
    15.    echo "Error: cannot get str";
    16. }
     
  5. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
  6. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
    It doesn't work too. I receive a blank string:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. public class UIManager : MonoBehaviour
    7. {
    8.     public InputField output;
    9.  
    10.     public void SaveStrOnClick()
    11.     {
    12.         StartCoroutine("SaveStr");
    13.     }
    14.  
    15.     IEnumerator SaveStr()
    16.     {
    17.         WWWForm form = new WWWForm();
    18.  
    19.         form.headers.Add("Access-Control-Allow-Credentials", "true");
    20.         form.headers.Add("Access-Control-Allow-Headers", "Accept");
    21.         form.headers.Add("Access-Control-Allow-Methods", "POST");
    22.         form.headers.Add("Access-Control-Allow-Origin", "*");
    23.         form.AddField("str", "Hello");
    24.  
    25.         WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
    26.         yield return www;
    27.  
    28.         try
    29.         {
    30.             output.text = www.text;
    31.         }
    32.         catch (System.Exception)
    33.         {
    34.             output.text = "Error in: output.text = www.text;";
    35.         }
    36.     }
    37. }
    38.  
     
  7. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    584
    You need to do that on your php file not unity code.
    The mine work
    Code (CSharp):
    1. header("Access-Control-Allow-Credentials: true");
    2. header('Access-Control-Allow-Origin: *');
    3. header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    4. header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
     
    Tabu and 8Observer8 like this.
  8. 8Observer8

    8Observer8

    Joined:
    Apr 29, 2015
    Posts:
    99
    Thank you very much! It works now :)

    I upload my WebGL application on another free hosting instead of Google Drive Hosting.

    This is my scripts:

    Unity side:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UIManager : MonoBehaviour
    6. {
    7.     public InputField output;
    8.  
    9.     public void SaveStrOnClick()
    10.     {
    11.         StartCoroutine("SaveStr");
    12.     }
    13.  
    14.     IEnumerator SaveStr()
    15.     {
    16.         WWWForm form = new WWWForm();
    17.         form.AddField("str", "Hello");
    18.         WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
    19.         yield return www;
    20.         output.text = www.text;
    21.     }
    22. }
    Server side:
    Code (CSharp):
    1. <?php
    2.  
    3. header("Access-Control-Allow-Credentials: true");
    4. header('Access-Control-Allow-Origin: *');
    5. header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    6. header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
    7.  
    8. if (!empty($_POST["str"]))
    9. {
    10.     $str = $_POST["str"];
    11.     echo "Ok, we get: ".$str;
    12. }
    13. else
    14. {
    15.    echo "Error: cannot get str";
    16. }
     
  9. ManguGames

    ManguGames

    Joined:
    Sep 13, 2019
    Posts:
    2
    :) It still works but the new class should be used. my code c# and php example:
    //Get method example
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class ManagerDb : MonoBehaviour {
    7.  
    8.   void Start() {
    9.     StartCoroutine(GetRequest("https://samplepage.000webhostapp.com/file.php"));
    10.   }
    11.  
    12.   IEnumerator GetRequest(string uri) {
    13.  
    14.     using(UnityWebRequest webRequest = UnityWebRequest.Get(uri)) {
    15.       // Request and wait for the desired page.
    16.       yield return webRequest.SendWebRequest();
    17.  
    18.       if (webRequest.isNetworkError) {
    19.         Debug.Log(webRequest.error);
    20.         //or example
    21.         GetComponent<Text>().text = "Not available.";
    22.       } else {
    23.         Debug.Log(webRequest.downloadHandler.text);
    24.      
    25.         //Example: component Text from inspector and show the result in game/unity
    26.         GetComponent<Text>().text = webRequest.downloadHandler.text;
    27.       }
    28.     }
    29.   }
    30. }
    Code (php):
    1. <?php
    2.  
    3. header("Access-Control-Allow-Credentials: true");
    4. header('Access-Control-Allow-Origin: *');
    5. header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    6. header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
    7.  
    8. //You get these data from your database
    9. $servername = "host";
    10. $username = "123456";
    11. $password = "123456";
    12. $dbname = "databasename";
    13.  
    14. // Create connection
    15. $conn = new mysqli($servername, $username, $password, $dbname);
    16. // Check connection
    17. if ($conn->connect_error) {
    18.     die("Connection failed: " . $conn->connect_error);
    19. }
    20.  
    21. $sql = "SELECT * FROM tableName";
    22. $result = $conn->query($sql);
    23.  
    24. if ($result->num_rows > 0) {
    25.     // output data of each row
    26.     while ($row = $result->fetch_assoc()) {
    27.         echo $row["columName"] . " : " . $row["otherColumname"];
    28.     }
    29. } else {
    30.     echo "0 results";
    31. }
    32. $conn->close();
    33.  
    You can use the page https://es.000webhost.com/ for free to host its File.php and create your database in the same place, or search for any other repository, I use https: //www.clever-cloud .com / en / for the free database.

    See more method GET, POST, PUT, DELETE from unity c# here
    documentacion https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html