Sending a high score to a mysql database?

Hi, I’ve been trying to write a simple high scores system for my wobsite, and so far I’ve been somewhat stuck on this. I’ve adapted the script from the Scripting Reference, but without much luck.

Here’s the script I’m using on the Unity end:

function GameOver() {
	// Create a form object for sending high score data to the server
var form = new WWWForm();
// Assuming the perl script manages high scores for different games
form.AddField( "game", "MyGameName" );
// The name of the player submitting the scores
form.AddField( "playerName", playName );
// The score
form.AddField( "score", score );

// Create a download object
var download = new WWW( highscore_url, form );

// Wait until the download is done
yield download;

if(download.error) {
print( "Error downloading: " + download.error );
return;
} else {
// show the highscores
Debug.Log(download.text);
}
	Application.LoadLevel("GameOver");
}

For the Perl script, this is what I have:

#!/usr/bin/perl
# The SQL database needs to have a table called highscores
# that looks something like this:
#
# CREATE TABLE highscores (
# game varchar(255) NOT NULL,
# player varchar(255) NOT NULL,
# score integer NOT NULL 
# );
#
use strict;
use CGI;
use DBI;

# Read form data etc.
my $cgi = new CGI;

# The results from the high score script will be in plain text
print $cgi->header("text/plain");

my $game = $cgi->param('game');
my $playerName = $cgi->param('playerName');
my $score = $cgi->param('score');

exit 0 unless $game; # This parameter is required

# Connect to a database
my $dbh = DBI->connect( 'DBI:mysql:toybox_gamehs', 'deusexmachina', '16weapons' )
|| die "Could not connect to database: $DBI::errstr";

# Insert the player score if there are any
if( $playerName  $score) {
$dbh->do( "insert into highscores (game, player, score) values(?,?,?)",
undef, $game, $playerName, $score );
}

# Fetch the high scores
my $sth = $dbh->prepare(
'SELECT player, score FROM highscores WHERE game=? ORDER BY score desc LIMIT 10' );
$sth->execute($game);
while (my $r = $sth->fetchrow_arrayref) {
print join(':',@$r),"\n"
}

However, I did a quick test of my game and then checked the database, and no entry had been written. :expressionless: Does anybody here know what the problem would be?

Also, the line, “Debug.Log(download.text);” is returning the error: "Assets/Scripts/ScoreDisplay.js(54,20): BCE0019: ‘text’ is not a member of ‘UnityEngine.WWW’. " Is this related?

Hi, welcome to the forum!

If you are getting error messages from the script, then it may not be compiled into a state where it is even sending the message - you need to get rid of any compile errors to be sure that the current version of the script is what is actually running. Check the console and see if you are getting any other errors. The message you are seeing appears to be misleading because WWW definitely has a variable called text.

Unfortunately, there are no other errors in the console, so I’m drawing a blank on that angle. :confused: Is it possible that, for some reason, Unity simply isn’t waiting until the download has finished?

cgi has a problem in parsing the header.
from what I recall you will need to remove the extra empty line at the end of the header manually that cgi otherwise parses incorrectly.

at least on U2 there was a special handling required in this case

Is the blank line to which you’re referring the one right after “use DBI;” ?

Alright, I’ve switched to a different series of scripts for dealing with high scores since I had no success with Perl, but I’m having trouble with two things(I’m working with the Server Side Highscores tutorial on the Unifycommunity wiki: I am unaware of what exactly I’m supposed to place in Mysql_host, and even after adding the md5functions.js script, my script still turns up the following: Assets/Scripts/Score.js(42,14): BCE0005: Unknown identifier: ‘Md5’.

The wiki page for Server Side Highscores does actually mention that you also need the MD5 script (also on the wiki) to use the script.

I’m aware of that, and I did add the MD5 script, which cleared up that error, but only when I directly placed its code IN the same script as the code that submits the high scores.

On the server side, though, I’m having issues with the code. I’ve simplified the addscore function in Unity to only provide the unencrypted name and score, but if it’s passed any name other than “name” it says that the current name is not a column. (If it’s passed “name=name” the entry is written as “anonymous”)

This is the code I’m currently using:

<?php

        $db = mysql_connect('localhost', 'deusexmachina', '16weapons') or die('Could not connect: ' . mysql_error());
        mysql_select_db('toybox_gamehs') or die('Could not select database');

        // Strings must be escaped to prevent SQL injection attack.
        $player = mysql_real_escape_string($_GET['name'], $db);
        $points = mysql_real_escape_string($_GET['score'], $db);
        echo $player;
        echo $points;

            // Send variables for the MySQL database class.
            $query = "insert into scores (name, score) values ($player, $points)";
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());
?>

Which version of Unity are you using? If you are still 2.6 then use www.data instead as that was renamed to www.text in v3

I’m running Version 2.6. Thanks for clearing that up!

Unfortunately, I’m still having trouble with the PHP script(which I am finding more understandable, but still having the same issue) where name entry is seemingly impossible. Can anybody explain to me why the script isn’t accepting any entries in the name field and instead searching for a column in the MYSQL table with the player’s name?

<?php

        $db = mysql_connect('localhost', 'deusexmachina', '16weapons') or die('Could not connect: ' . mysql_error());
        mysql_select_db('toybox_gamehs') or die('Could not select database');

        // Strings must be escaped to prevent SQL injection attack.
        $player = mysql_real_escape_string($_GET['name'], $db);
        $points = mysql_real_escape_string($_GET['score'], $db);
        echo $player;
        echo $points;
if ($points > 10000){
            // Send variables for the MySQL database class.
            $query = "insert into scores (playername, score) values ($player, $points)";
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
?>