Basic online high scores with XNA / C# and PHP / MYSQL

Basic online high scores with XNA / C# and PHP / MYSQL

Basic online high scores with XNA / C# and PHP / MYSQL

15 Comments on Basic online high scores with XNA / C# and PHP / MYSQL

In this tutorial I want to give you a very straight forward and basic way to implement online high scores (aka. online leaderboards or online highscore tables) for XNA Game Studio PC games. We used a similar aproach to quickly implement basic online highscores for an early version of our game zeit2. I assume that you know how to work with C#,PHP,HTML and MySQL for this little tutorial. Of course in a real setting you would probably want to implement a user management/login to identify every unique user. But to make it simplier for now we only submit a name to be stored with every score.
What you need is a PHP server with MySQL running as a database backend.

STEP 1 – Setting up the database

First you will have to create a MySQL Table:
CREATE TABLE IF NOT EXISTS `mygame_highscores` (
`ID` int(11) NOT NULL auto_increment,
`ModeID` varchar(256) NOT NULL,
`Name` varchar(256) NOT NULL,
`Info` varchar(256) NOT NULL,
`Score` int(11) NOT NULL,
`Timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,

ID contains an auto-incremented Number that is unique for every entry.
ModeID contains an identifier for which mode or level a highscore was achieved. This way you can save multiple highscores tables in one MySQL table.
Name is the name of the player that got the new score. (In an advanced version you would use an UserID that refers to another table in your database)
Info contains anything you wanna store with your score, i.e. what time the user needed to finish a level.
Score has the number that represents the score the player got.
Timestamp lets you identify when a score was saved. That way you can have weekly leaderboards for example.

Let’s enter some dummy data to our table. this example only inserts two records but feel free to add more for better testing.

INSERT INTO `databasename`.`mygame_highscores` (
`ID` ,
`ModeID` ,
`Name` ,
`Info` ,
`Score` ,
NULL , '1', 'Marcus', '10:30 min', '100',
), (
NULL , '1', 'Peter', '9:12 min', '90',

Read on the next page how we write our PHP script to request scores.

Pages: 1 2 3 4 5 6 7


  1. Rahul  - December 23, 2009 - 15:36

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    Thanks again

  2. Fer  - January 22, 2010 - 00:13

    So useful, a } is needed at the end of requestscore.php, it returns a error, and i don’t know why i made 2 entries on that table in my database and when i click get score it just returns SERVER_, i’ve looked everything and i don’t know what happens, revise the code please because i don’t know neither php mysql and i can’t see complex errors, but for some reason i doesn’t work for me, could you help????

  3. Thomas Bedenk  - January 22, 2010 - 02:39

    I will try to find some time to look through the code, but we are currently very busy. Its hard to get these things completely right if you don’t know at all about PHP, since there are some adjustments necessary to the code. If you only see SERVER_ returned that is an indication for no real errors, but missing database entries or empty query results. MAKE SURE that whatever you entered in your database entries as ModeID is also send in the query. So if you use requesttest.html to test the php file, what ever you enter in the field ModeID (a string) needs to be in that column of your database entries. Hope that helps. I corrected the }, thanks for that.

  4. Fer  - January 22, 2010 - 18:36

    well i know something about php, not very good but somthing i didn’t just copy the code, i looked at it and i more or less understand what it does, ModeID entries are 1 for sure, and in your requesttes.htm modeid i write 1 for sure, it just write SERVER_ i will continue with the tut to see f it works at last but thankyou very much anyway, great tutorial

  5. William Arends  - March 14, 2010 - 05:32

    On the post code for the modeID your missing the _ it should be .$_POST[…]. , I had to fix this to get working for me sorry if someone already posted this typo=) it is above ModeID='”.$POST[“ModeID”]. This is missing the _ . I am almost sure.

  6. Thomas Bedenk  - March 29, 2010 - 16:07

    Thank you for this remark! It was in fact a typo. I corrected it in the text.

  7. Burbruee  - July 29, 2010 - 18:27

    Few problems while testing this from page 5 when starting to write actual code in C#.

    First, in the webPost method. You write responseString = null; which I had to change to string responseString = null;
    Later on, I see Console.WriteLine(responseFromServer); but this responseFromServer variable has not been declared. What is it? I changed it to responseString, not sure if that’s what you meant or if you left something out. Then by the end of the method, there are two closing brackets, (but only one opening bracket in the method) I removed the one before the return value. Works fine.

    So far so good. What I’m not getting though is this:
    public highscoreTable getScores(string modeid, string format)
    private highscoreTable parseToHighscoreTable(string tableString)
    and this
    return new highscoreTable(ranks, names, infos, scores);
    You appear to have a “highscoreTable” class? which is not explained at all in the article.

    It would be helpful if you could provide a full working sourcecode, or at least some answers to my questions. Thanks, I really like the article apart from the problems I’m having.

  8. Bethann Rauelo  - January 22, 2011 - 14:04

    yeah bookmaking this wasn’t a bad determination outstanding post! .

  9. Alex Suth  - February 12, 2011 - 01:50

    Haha! Got it to work! Thank you very much! My minor tweaks were:

    1. Changed responseString = null; to String responseString = null;

    2. Added using System.Net;
    using System.Text.RegularExpressions;
    to my using statements

    3. Removed the Console.WriteLine(responseFromServer); line

    4. Renamed all highscoreTable classes to highScoreTable.

    5. Created a simple class for highScoreTable:
    class highScoreTable
    string[] ranks = new string[10];
    string[] names = new string[10];
    string[] infos = new string[10];
    int[] scores = new int[10];

    public highScoreTable(string[] ranks, string[] names, string[] infos, int[] scores)
    this.ranks = ranks;
    this.names = names;
    this.infos = infos;
    this.scores = scores;

    Then a getScores(“1”, “TOP10”) call fetches me my highScores!

    Thanks again!

  10. Robert LaForge  - September 11, 2011 - 22:59

    Any thoughts on how to modify this code for a Windows Phone 7 XNA game?

  11. Alex  - October 10, 2012 - 17:01

    I keep getting this error for the getScores method:
    Inconsistent accessibility: return typeis less accessible than method

  12. Xavier Lebec  - May 28, 2013 - 07:50

    Thanks so much for this tutorial!

  13. Vinnie Vengeance  - October 5, 2013 - 10:56

    Very handy – thanks very much 🙂
    I had to port it to VB/ASP however have come up with a good way of “hiding” the post and request pages (for ASP/VB anyway):
    1). In the request/post application code include the following command:
    Request.UserAgent = “NameOfYourAppV1”
    2). On the 2 webpages (request/post) include the following command:
    if request.servervariables(“HTTP_USER_AGENT”) “NameOfYourAppV1” then
    end if
    This will ensure if a web browser or a spider etc .. goes to your “hidden” page(s) it will be redirected never seeing the output. If anyone has further feedback I’d be keen to hear.


  14. Anonymous  - August 18, 2014 - 21:49

    […] ich mir jetzt ne MySql Datenbank und Webspace geholt. Wollte dann nach diesem tutorial vorgehen: Basic online high scores with XNA / C# and PHP / MYSQL | Brightside Games Aber irgendwie bekomm ich den Nutzer bzw. die richtigen Rechte nicht korrekt hin und bekomme daher […]

  15. Anonymous  - September 19, 2014 - 16:59

    $dbResult = mysql_query("SELECT * FROM mygame_highscores WHERE ModeID='".$_POST["ModeID"]."' ORDER BY Score DESC LIMIT 10");

    please escape user input!!!

    $dbResult = mysql_query("SELECT * FROM mygame_highscores WHERE ModeID='".mysql_real_escape_string($_POST["ModeID"], $db)."' ORDER BY Score DESC LIMIT 10");

Leave a comment


Brightside Games UG (haftungsbeschränkt)
Am Studio 2 A | D-12489 Berlin
phone +49 (0) 177 335 4447

Geschäftsführer: Thomas Bedenk, Johannes Giering
Sitz der Gesellschaft: Berlin
Amtsgericht Charlottenburg HRB 127560 B
USt-IDNr.: DE 271 848 653

Back to Top