Getting data from a database and making a scoreboard with it (C#)

Hi!

I need to make a simple scoreboard with the data I will get from a database.

I have a script for getting the data (it’s not complete as you can see):

public class GetDataFromDatabase : MonoBehaviour
{
    public void CallDataRequest()
    {
        StartCoroutine(DataRequest());
    }

    IEnumerator DataRequest()
    {

        using (UnityWebRequest www =
UnityWebRequest.Get("https://knnnj.000webhostapp.com/dbgetdata.php"))
        {
            yield return www.SendWebRequest();
        }

    }
}

I just can’t seem find a tutorial for my case or maybe I’m just blind.

This code will run if the player clicks on a button, then a panel will show with the data I will get from the script.
I suppose I should put the data in a list.

Thanks in advance! :slight_smile:

I tried your request from a browser and didn’t see any data. You’ll probably want to return some test data to get started. This might help https://stackoverflow.com/questions/46003824/sending-http-requests-in-c-sharp-with-unity The first step is to make sure you retrieve your data, then you can work on the logic to get the returned data into a list.

There are plenty of tutorials out there, they just might not be “How to get data from a database and display as a scoreboard in c#” tutorials.

Always break down each step and then search for the tutorials on how to do those steps.

Downloading data
How to retrieve data from the downloaded data
How to convert that data into a list
How to populate text fields
etc.

While these may not be the exact steps, it should give you an idea. Don’t build the car, build each part and then put it together to make a car.

That’s because as I said the script is half done. It’s just sends a request but it doesn’t do anything. Thanks for the reply!

It’s not in the script, it’s your server that isn’t sending anything in response. Get that working first.

Of course! I searched it up in many ways and they are helpful to some degree but it’s always just not what I need. The breaking down is a good idea and that sentence is nice at the end, thanks for the reply! :slight_smile:

That’s weird it should work. It works for me. I have another script where I insert data into the database and that works.

public class DatabaseConnection : MonoBehaviour
{
    public InputField nameField;
    public InputField scoreField;
    public Button saveDataButton;

    public void CallInsertion()
    {
        StartCoroutine(Insertion());
    }

    IEnumerator Insertion()
    {

        WWWForm form = new WWWForm();
        form.AddField("nev", nameField.text);
        form.AddField("pontszam", scoreField.text);

        using (UnityWebRequest www = UnityWebRequest.Post("https://knnnj.000webhostapp.com/dbadd.php", form))
        {
            yield return www.SendWebRequest();
        }

    }

}

You can try it yourself, browse to https://knnnj.000webhostapp.com/dbgetdata.php in a browser (to perform a GET). I don’t see any data returned. What do you see?

Yes, my bad! I tested it in Unity and also viewed it in the browser and I don’t see anything either. My php file is the problem here I’m convinced. Thanks for helping.