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

Send variable from C# to php for MySQL

Discussion in 'Scripting' started by Trese759, Feb 27, 2017.

  1. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    Hi, i'm new to MySQL system. I have to store in a database:

    • Username;
    • Score;
    • Resources.

    For send username, score and resources i don't have any problems but whene i try to get resources from database i have this error:

    </style>
    </head>

    <body>
    <h1>Bad request!</h1>
    <p>


    Your browser (or proxy) sent a request that
    this server could not understand.

    </p>
    <p>
    If you think this is a server error, please contact
    the <a href="mailto:postmaster@localhost">webmaster</a>.

    </p>

    <h2>Error 400</h2>
    <address>
    <a href="/">localhost</a><br />
    <span>Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1</span>
    </address>
    </body>
    </html>


    UnityEngine.Debug:Log(Object)
    <SetPlayerName>c__Iterator1:MoveNext() (at Assets/Scripts/DataBase/MySQL/DBLoader.cs:67)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    This is my php file:

    Code (JavaScript):
    1. <?php
    2.  
    3.     $servername = "host";
    4.     $username    = "name";
    5.     $password    = "";
    6.     $dbname        = "game_name";
    7.  
    8.     $coinsname     = (isset($_REQUEST['coinsnameGet']) ? $_REQUEST['coinsnameGet'] : null);
    9.  
    10.     //Create Connection
    11.     $connection = new mysqli($servername, $username, $password, $dbname);
    12.  
    13.     //Check Connection
    14.     if(!$connection) {
    15.         die("Connection Failed. " . mysqli_connect_error());
    16.     }
    17.      
    18.     $sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
    19.     $result = mysqli_query($connection, $sql);
    20.  
    21.     while($row = mysqli_fetch_array($result, MYSQLI_BOTH) {
    22.         echo $row['Coins'];
    23.     }
    24. ?>
    I have to check if name is == to local database name then get resources.
    The local db is made with sqlite.

    And this is my connected class:

    Code (CSharp):
    1. private DataBase dbLocal;
    2.  
    3.     WWW userData;
    4.     WWW scoreData;
    5.     WWW coinsData;
    6.  
    7.     public Text Coins;
    8.  
    9.     private string[] dbUsers;
    10.     private string[] dbScores;
    11.  
    12.     private string dbCoins;
    13.  
    14.     private string postCoinsData = "http://host/game_name/UsercoinsData.php";
    15.  
    16.     public GameObject scorePrefab;
    17.     public Transform scoreParent;
    18.  
    19.     public GameObject rankValue;
    20.     public GameObject nameValue;
    21.     public GameObject scoreValue;
    22.  
    23.     // Use this for initialization
    24.     IEnumerator Start () {
    25.  
    26.         dbLocal = (DataBase)FindObjectOfType(typeof(DataBase));
    27.  
    28.         userData = new WWW("http://host/game_name/UsernameData.php");
    29.         scoreData = new WWW("http://host/game_name/UserscoreData.php");
    30.  
    31.         yield return userData;
    32.         yield return scoreData;
    33.  
    34.         string textUserData = userData.text;
    35.         dbUsers = textUserData.Split(';');
    36.  
    37.         string textScoreData = scoreData.text;
    38.         dbScores = textScoreData.Split(';');
    39.      
    40.         scoreParent.transform.localPosition = new Vector3(0, 0, 0);
    41.  
    42.         dbLocal.Connection();
    43.  
    44.         StartCoroutine(SetPlayerName());
    45.         GenerateScore();
    46.     }
    47.  
    48.     IEnumerator SetPlayerName()
    49.     {
    50.         string setName = postCoinsData + "coinsnameGet = " + WWW.EscapeURL(dbLocal.GetName());
    51.         WWW dataCoins = new WWW(setName);
    52.  
    53.         Debug.Log(dbLocal.GetName());
    54.  
    55.         yield return dataCoins;
    56.  
    57.         string textDataCoins = dataCoins.text;
    58.         Coins.text = textDataCoins;
    59.         Debug.Log(Coins.text); //This log the error
    60.     }
    Thanks for any helps!
     
  2. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    does the php work all by it's self. will it echo the results?I can send you my scripts if you need something to copy. I used form post to get my scripts working. once i had the php working, then I knew the issue was with unity scripts.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What @johne5 said. You need to test each layer separately.

    Start with the database. Does the information actually exist? Does the login have the right persmissions to access it.

    Then try your PHP. Can you echo the correct result in a browser?

    Then try in Unity. Start by just debugging the raw output from the WWW. Then move to trying to parse it.

    Also note your PHP is vulnerable to SQL injection. You don't want this.
     
  5. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    Actually in the browser I get this output:

    Parse error: syntax error, unexpected 'echo' (T_ECHO) in C: \ xampp \ htdocs \ infinity_launch \ UsercoinsData.php on line 22

    But I do not understand why. (I'm new in php).
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Line 21 is missing a closing bracket.
     
  7. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    Obviously... A typical stupid error... Thx for this, but the problem in Unity persist.

    1. There is a method for connect unity game with an server side database without using php?
    2. If yes, can anyone indicate how or some tutorials?
    Can you make a simple example? Thx.

    From MySQL DB i get the player and the score and i can see in game.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Really you can do a WWW to any web endpoint. And then you can parse the response back in Unity. Unity doesn't actually care where you send it. So you can build your server in any language you like.

    You are probably better off looking at some generic web programming tutorials.

    So does the php work now? Do you get a meaningful response if you use the browser?
     
  9. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    In the browser i now see a blank page, probably becouse the $coinsname variable it's store in a local database made with sqlite. Essentially when game launch:

    Code (CSharp):
    1. public GameObject username //InputField
    2.  
    3. Start() {
    4.  
    5.      if(sqliteDB.GetName() == null) {
    6.          username.SetActive(true);
    7.      }
    8.      else if(sqliteDB.GetName() != null) {
    9.          username.SetActive(false);
    10.      }
    11. }
    This is for store name in MySQL DB once only. Can make this the error? The best way for make this is store the player name in a game variable instead on the local DB?
     
  10. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
  11. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    Another test in php file:

    If i change this:

    Code (JavaScript):
    1. $sql = "SELECT Coins FROM score WHERE Name = '" . $coinsname . "'";
    with this:

    Code (JavaScript):
    1. $sql = "SELECT Coins FROM score WHERE Name = 'PlayerName'";
    In brower i see the score.
     
  12. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
  13. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36
    There isn't a solution?
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is. We just have very few experts in php and Unity who hang out on the forums.

    Which tells me your php is still wrong. Can you simply get the php to echo the variable you pass in via the URL?
     
  15. Trese759

    Trese759

    Joined:
    Nov 27, 2016
    Posts:
    36