Trying to get float to MySQL, it gets rounded.

Hello guys,

I want to use a PHP-file to get new scores into MySQL. But it always rounds the float.
I already tried to change the comma to a dot. Didn’t work.
In the MySQL table when manually adding a float with a dot, it works.

This is the essential .PHP part

    // Get the user's name and store it
    $name = mysqli_escape_string($connection, $_POST['name']);
    $name = filter_var($name, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
    // Get the user's score and store it
    $score = $_POST['score'];

    // Create prepared statement
    $statement = $connection->prepare("INSERT INTO highscoreTableMySQL (name, score) VALUES (?, ?)");
    $statement->bind_param("si", $name, $score);

    $statement->execute();
    $statement->close();

This is the Unity-Script

    public void PostScores(string name, float score)
    {
        StartCoroutine(DoPostScores(name, score));
    }

    IEnumerator DoPostScores(string name, float score)
    {
        WWWForm form = new WWWForm();

        form.AddField("name", name);
        form.AddField("score", score.ToString().Replace(",", "."));
    }

What goes out over the wire is just pure text.

It is up to you to format it properly for whatever your backend is expecting.

Anything going over the network can be debugged like this:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

1 Like

Don’t do this:

score.ToString().Replace(",", ".")

Use the invariant culture instead use

.ToString(CultureInfo.InvariantCulture)

(requires using System.Globalization; at the top)

Apart from that, you specify "si" as the parameter types, so you restrict the first parameter to be a string and the second to be an integer. You probably want to use either "sd" or "ss".

I barely use the mysqli interface and just use the standardized PDO interface that php provides. There you usually don’t have to provide type strings and you usually would use named variables inside the query. Though that’s up to you.

Of course there could be other issues on the line. For example what’s the actual datatype of the score column?

1 Like

That solved my problem.
Thank you very much…