Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Online highscore db using MySQL php - I'm out of things to try

Discussion in 'Multiplayer' started by MadRobot, Dec 26, 2012.

  1. MadRobot

    MadRobot

    Joined:
    Jul 12, 2011
    Posts:
    339
    Can anyone please tell me what I am doing wrong? I've been working on this for several days. I've googled and searched everything I can find. I've scoured UnityAnswers, Unity Forums, the Unity Script Reference, the Unity Manual, and the Unity API Reference. I have downloaded 3 different HighScore packages from the Unity Asset Store and gone through their code. I have done and checked everything I could find. I can't find anything wrong.

    I am getting several errors, neither consistently. The first is the 'crossdomain.xml' policy file not found error. The second is the return data is empty. I know the MySQL db is setup and populated with data as we have been able to test it out manually and retrieve our highscores.

    Everything is located on the same server. This tells me I shouldn't need crossdomain.xml, but I have one anyway because it wasn't working without it. Still isn't.

    crossdomain.xml looks like this. I edited it using Notepad and use Save As to ensure it's encoding is UTF-8.
    Code (csharp):
    1.  <?xml version="1.0" encoding="UTF-8"?>
    2. <cross-domain-policy>
    3. <allow-access-from domain="*"/>
    4. </cross-domain-policy>
    The crossdomain.xml file is located on the root of the server, where it's supposed to be.

    We use this C# code to make the request for the highscores.
    Code (csharp):
    1.  StartCoroutine (GetScore());
    We use this C# code to execute the request.
    Code (csharp):
    1.  public IEnumerator GetScore() {
    2.   int retrieveLimit = 10;
    3.   int level = 1;
    4.   WWWForm form = new WWWForm();
    5.   form.AddField ("limit", retrieveLimit);
    6.   form.AddField ("level", level);
    7.   WWW www = new WWW("www.sampleserver.com/path/getHighscore.php", form);
    8.   yield return www;
    9.   // do stuff with www.text (shown below)
    10. }
    getHighscore.php is located in the same folder with the game on the server. To pull highscores we are using this php code to talk to a MySQL db:
    Code (csharp):
    1.  $query = "SELECT * FROM ScoreTable WHERE level=$level ORDER BY score ASC LIMIT $limit";
    2. $result = mysql_query($query);
    When we go to read the retrieved highscores, we find nothing. (C#)
    Code (csharp):
    1.  String s = www.text;
    2. Debug.Log ("found: " + s); // shows nothing
    Or we get the 'crossdomain.xml' policy file not found error.

    What's left to try?

    Thanks
     
  2. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    394
    Depending if you are using PC or MAC, you may want to grab a snitching program to see if Unity really is trying to get that file or not. For windows, get "fiddler" for Mac get HTTPScoop.
     
  3. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    394
    I also noticed your C# code says:
    "www.sampleserver.com/path/getHighscore.php

    Shouldn't this be http://www. whatever your server is .com ???


    I also recommend using my site http://dreamlo.com which is a free online message board, sample code in the asset store, no need for php, sql, etc. :)
     
  4. MadRobot

    MadRobot

    Joined:
    Jul 12, 2011
    Posts:
    339
    Yea, my code did have the http in it, I just failed to type it above by accident.

    Thanks for the offer to look at your work, but I already had :) I'd downloaded several highscore packages from the asset store, as well as examined three or four additional pieces of code around the internet. I'd check the script reference, manual, unity answers, searched the forums here, etc, etc, etc, etc, etc.

    Anyway. It was a red herring. The problem arose because we have two server 'addresses'. One is the web address, the other is a virtual address used for the data stream. The C# code needed to be aimed at the web address, the php code needed to point at the data stream. I had mistakenly thought they all needed to be the same thing, so whenever I changed it, I would change all of them. This resulted in the client sporadically finding the crossdomain policy because half the time it wasn't pointed at the correct address.

    So, problem solved. Thanks for your help!