• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Sending a high score to a mysql database?

Status
Not open for further replies.

TBM_Christopher

Semi-pro Game Dev
  • 448
    Posts
    14
    Years
    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 Unity Scripting Reference, but without much luck.

    Here's the script I'm using on the Unity end:
    Code:
    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:
    Code:
    #!/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', '-Password Censored-' )
    || 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. :-| 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?
     

    shortymant

    _+Bow Down+_
  • 16
    Posts
    14
    Years
    When debugging, it would be best to have an if/else try/catch block on everything.
    Such as this line;
    Code:
    my $dbh = DBI->connect( .....stuff....... ) || die "Couldn't connect."

    When I was messing with CRecordset in C++, it took me a few days to discover a tiny error in my connection code - even though I would never see the MessageBox "Couldn't connect to the Database.".

    I have never touched Perl, so I can't say off the back of my hand why it isn't working. You could take a look into this: articles.sitepoint.com/article/access-mysql-database-perl but I don't know the deal with Perl and Unity. (Sorry about the link; "You are only allowed to post URLs to other sites after you have made 15 posts or more.")
     

    Poeman

    Banned
  • 755
    Posts
    15
    Years
    • Age 29
    • Seen Nov 1, 2012
    The bumping rule is 3 months now, right? Either way I feel this is a legitimate bump because I will be addressing the question, and answering it.

    Firstly I do not know Unity, so I can't check your script there, but I CAN tell you it's insecure, you should never open up a database directly in a program. Instead, have a page of PHP in-between.

    Have Unity download the PHP page of whatever.php?variable=value
    The blue part, is important, it is the get method for a value. The issue with this method though, is the fact that anyone can open the whatever.php page, with the variable being set to anything! So to fix that, you encrypt the value on the unity end, and unencrypt it with PHP, then submit a query to the MYSQL database.

    Any more questions, feel free to contact me about this subject.
    (No, I don't want to help with your project.)
    (Yes reader that means you)
    (Only put this here because I used to get spammed with help requests)
     

    Cilerba

    the hearts of lonely people
  • 1,162
    Posts
    14
    Years
    The rule is actually 2 months. Next time try not to bump a thread even if you do think it's a legitimate bump.

    -locked-
     
    Status
    Not open for further replies.
    Back
    Top