WebGL and MySQL

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

<?xml version="1.0" encoding="ASCII"?>
<cross-domain-policy>
<allow-access-from domain="*" secure="false"/>
</cross-domain-policy>

Thank you for your replay in advance

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:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    public InputField output;

    public void SaveStrOnClick()
    {
        StartCoroutine("SaveStr");
    }

    IEnumerator SaveStr()
    {
        WWWForm form = new WWWForm();
        form.AddField("str", "Hello");
        WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
        yield return www;
        output.text = www.text;
    }
}

Server side:

<?php

if (!empty($_POST["str"]))
{
    $str = $_POST["str"];
    echo "Ok, we get: ".$str;
}
else
{
   echo "Error: cannot get str";
}

Fixe your CORS in your php file.

1 Like

Thanks!

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

<?php

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Origin: *');

if (!empty($_POST["str"]))
{
    $str = $_POST["str"];
    echo "Ok, we get: ".$str;
}
else
{
   echo "Error: cannot get str";
}

I found same problem here: WebGl build with WWW POST data is blank on the server - Questions & Answers - Unity Discussions

It doesn’t work too. I receive a blank string:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class UIManager : MonoBehaviour
{
    public InputField output;

    public void SaveStrOnClick()
    {
        StartCoroutine("SaveStr");
    }

    IEnumerator SaveStr()
    {
        WWWForm form = new WWWForm();

        form.headers.Add("Access-Control-Allow-Credentials", "true");
        form.headers.Add("Access-Control-Allow-Headers", "Accept");
        form.headers.Add("Access-Control-Allow-Methods", "POST");
        form.headers.Add("Access-Control-Allow-Origin", "*");
        form.AddField("str", "Hello");

        WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
        yield return www;

        try
        {
            output.text = www.text;
        }
        catch (System.Exception)
        {
            output.text = "Error in: output.text = www.text;";
        }
    }
}

You need to do that on your php file not unity code.
The mine work

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
2 Likes

Thank you very much! It works now :slight_smile:

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

This is my scripts:

Unity side:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    public InputField output;

    public void SaveStrOnClick()
    {
        StartCoroutine("SaveStr");
    }

    IEnumerator SaveStr()
    {
        WWWForm form = new WWWForm();
        form.AddField("str", "Hello");
        WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form);
        yield return www;
        output.text = www.text;
    }
}

Server side:

<?php

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');

if (!empty($_POST["str"]))
{
    $str = $_POST["str"];
    echo "Ok, we get: ".$str;
}
else
{
   echo "Error: cannot get str";
}

:slight_smile: It still works but the new class should be used. my code c# and php example:
//Get method example

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ManagerDb : MonoBehaviour {

  void Start() {
    StartCoroutine(GetRequest("https://samplepage.000webhostapp.com/file.php"));
  }

  IEnumerator GetRequest(string uri) {

    using(UnityWebRequest webRequest = UnityWebRequest.Get(uri)) {
      // Request and wait for the desired page.
      yield return webRequest.SendWebRequest();

      if (webRequest.isNetworkError) {
        Debug.Log(webRequest.error);
        //or example
        GetComponent<Text>().text = "Not available.";
      } else {
        Debug.Log(webRequest.downloadHandler.text);
      
        //Example: component Text from inspector and show the result in game/unity
        GetComponent<Text>().text = webRequest.downloadHandler.text;
      }
    }
  }
}
<?php

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');

//You get these data from your database
$servername = "host";
$username = "123456";
$password = "123456";
$dbname = "databasename";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM tableName";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        echo $row["columName"] . " : " . $row["otherColumname"];
    }
} else {
    echo "0 results";
}
$conn->close();

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 Unity - Scripting API: UnityWebRequest