- Home /
In unity web player, how i can connect to mysql server by php?
i want to connect to mysql server. and it works when i build my project as pc, mac & linux standalone mode. however it does not work in web palyer mode. it dose not send any data to mysql server.
it is my c# code.
using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System; using System.Collections; using System.Collections.Generic;
public class DBManager : MonoBehaviour {
private string inname;
private int inscore;
public void PressButton()
{
GameObject NameIn = GameObject.Find ("InputName");
GameObject DisText = GameObject.Find ("TotalScoreText");
InputField INValue = NameIn.GetComponent<InputField> ();
GUIText GDT = DisText.GetComponent<GUIText> ();
int.TryParse (GDT.text, out inscore);
Debug.Log (inscore);
inname = INValue.text;
Debug.Log (inname);
Awake ();
}
private void Awake()
{
StartCoroutine (SubmitHighScore ());
}
private IEnumerator SubmitHighScore()
{
int _score = inscore;
string _name = inname;
WWW webRequest = new WWW ("http://my ip address/insertScore.php?Name=" + _name + "&Score=" + _score);
yield return webRequest;
}
and it is my php code
$link = mysql_connect('localhost', 'genie', 'genie');
mysql_select_db('run_score');
$name = @$_GET['Name'];
$score = @$_GET['Score'];
if(!$name)
die("No name.");
if(!$score)
die("No score.");
$data = $name."_".$score;
mysql_query("INSERT INTO ranking (Name, Score) VALUES ('$name', '$score')");
echo "success";
mysql_close($link);
Is it a sandboxing problem? Without any Player.log data it's impossible to know what's not happening.
Answer by DavidZendle · Jun 17, 2015 at 09:29 PM
It sounds like you're running into Unity's infamous Webplayer sandboxing issues.
Basically, Webplayer builds will only talk via WWW to domains with valid crossdomain policies.
The fix?
A quick test to see if this is your problem is to put a file called 'crossdomain.xml' on the root of the server hosting your php. This file should contain the following code:
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
If this fixes the problem, you can go back and replace the "*" with the specific domain that you're trying to access the php from to make things more secure.
Your answer
