Sending Google Play Game information to MySQL database

Hi,

I am trying to get some information from Google Play Services and send it to a MySQL database. Now I can get the ID and Username…but I can’t send it to the database. Following my C# script and php scripts (I changed some variables like server, database stuff for this example):

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

public class ShowUserInfo : MonoBehaviour
{
    public Text UserNameText;
    public Text UserIDText;
    public Text EmailText;
   
    void Awake()
    {
        Social.localUser.Authenticate (ProcessAuthentication);
    }

    void Start()
    {
        RegisterUser(UserIDText.text, UserNameText.text);
    }

    private void ProcessAuthentication (bool success)
    {
        if (success)
        {
            UserNameText.text = Social.localUser.userName;
            UserIDText.text = Social.localUser.id;
        }
        else
        {
            Debug.LogWarning ("Failed to authenticate");
        }
    }
   
    public IEnumerator RegisterUser(string loginUserID, string loginUserName)
    {
        WWWForm form = new WWWForm();

        using (UnityWebRequest www = UnityWebRequest.Post("http://server.com/app/insert.php", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log(www.downloadHandler.text);
            }
        }
    }
}
<?php
    $servername = "mydatabaseserver";
    $username = "databaseusername";
    $password = "databasepass";
    $dbName = "databasename";
   
    $loginUserID = $_POST["loginUserID"];
    $loginUserName = $_POST["loginUserName"];
   
    //Make Connection
    $conn = new mysqli($servername, $username, $password, $dbName);
    //Check Connection
    if(!$conn) {
        die("Connection Failed. ". mysqli_connect_error());
    }
   
    $sql = "INSERT INTO users (userid, username) VALUES ($loginUserID, '" . $loginUserName . "')";
    $result = mysqli_query($conn, $sql);
    echo ($sql);
?>

Thanks for any help on this.

Your code looks correct. What is the output of Debug.Log? Also, when debugging web requests it is usefull to have some packet analyzer to see what’s really going on the network side. I suggest wireshark (advanced) or fiddler (intermediate).

Hi, palex-nx. Thanks for your reply. I am really new to Unity. I couldn’t find a way to debug this while playing the game yet… I am not sure if the way I did to pass the variable values to the php script are correct. The log I got from Unity is that the variable are not going to the php…:

INSERT INTO users (userid, username) VALUES (, ‘’)

If you see this in unity logs then it seems your script worked to the last line without any errors. This echo ($sql); code at line 19 returned that string to you. Your problem is what you created the form but havent filled it with data. That’s why fields in the VALUE clause are empty, wich is wrong. You must fill all the fields in the form with data before sending the query, like this:

form.AddField("loginUserID", loginUserID.ToString());
1 Like

Thanks… I will try that.

hello is there any ways that we get email of every user and store it?