Creating your own Leaderboard / Highscores Easy and Free (PHP & MySQL) [UPDATED 2020]

  • 55 favourites


Attached Files

The following files have been attached to this tutorial:



Download now 180.37 KB


27,262 visits, 74,266 views


Creating a Database (MySQL)

We are going to create a database. A database is an organized collection of data. This is very helpful to store highscores, usernames and many other information useful in your games. MySQL is a powerful language to create and manage databases.

Log in to your account, access your Dashboard ("Manage Website"). In Tools go to Database Manager and create a New Database.

In the new window, you will create a database: with a database user and a database password. You can fill this data as you want, but I recommend you to copy the same info as shown here (so you can follow better the tutorial then you can change it later):

-database = db

-user = user

-password = ******** (whatever you want but don't forget it. You'll need this in a few minutes)

Notice that your complete database name is not 'db', it is something like id13353839_db. Also, your username is not 'user', it's again id13353839_user.

Congratulations, you have created your first database!

Creating a Database Table

Databases are a collection of data organized in tables. Every table saves data about something, like data about "users", data about "scores", data about "friends", etc...

Our recently created database is right now empty, we have to create our first table. Let's see how to create a table...

Go back to your Control Panel and enter phpMyAdmin.

A new phpMyAdmin window will open up. We can see that there are no tables created yet. But we are about to add the first one.

Click the tab called SQL to write SQL code. Paste the following code and click "GO":


Now a new table has been created. This code tells MySQL to create a table with three values: ID, NAME and SCORE. So for every user sending a score to the database, we will store his name and his score.

Notice how on the left side now we have a table: 'scores'. We can also see the structure of this table...


We have now our table ready so we can start writing and reading scores from PHP files!

(Manually) Inserting your first row

Tables are composed by 'rows'. Every row has information about the same subject. In the 'scores' table, every row must contain 3 fields: id, name and score.

For example one row is (ignore id): "JohnSmith 815"

We are going to manually add a row for testing purposes. Click the Insert tab and add the following information to the new row, then click the "Go" button:

If you click the Browse tab you can see all the information stored in this table. In other words: you can read all the scores submitted !!!

We have manually added some data to our table. Now we will see how to write and read scores from PHP files. Read on...


  • Order by
Want to leave a comment? Login or Register an account!
  • Great tutorial for a refresher. Ive just finished reading through the tutorial and will soon be implementing the PHP.

  • [FIXED] The scripts save/get should be updated according to the latest PHP formatting rules. After fiddling after a bit I found that in the save.php you should remove the "id" parts when inserting the score.

  • Hi! Thanks a lot for this tutorial and for giving us the example projects and folders! This is really what users want!

    Could you please explain how could I improve/edit the design of the leaderboard tables? What would be the method? I there a way I can create the design on a software like Dreamweaver and have this php/mysql leaderboard implemented? Ou maybe even edit the design as a page/post on Wordpress?

    Lastly, would you be wiling to help me doing this for a work project of mine? Off course I'll be glad to pay something.

    All the best,

  • Great tutorial! Worked great!

    At first, I wanted my leaderboard to update real-time, requesting the table about once per second, but then my website got shut down for requesting 1000 times more than I was allowed too! Oops!

    Anyways, thanks for the great tutorial!

  • Hi

    Thank you so much for this tutorial. I've managed to get everything working except for 2 issues:

    1: My leaderboard is only showing the top 5 results (the other 4 lines on screen are just showing the default 0 0.

    2: When I sort the results into ASC order (as I want to show the fastest time as the best score) the rank is always wrong by 1. I played the game for the first time and submitted my score and my rank came up as 2. This continues to be wrong with each new entry. I've checked my database and there are the correct number of entries. Am I doing something wrong? How can I amend it so it is correct?

    Can you help? I'd be so grateful!

    • Hey! Glad you liked it!

      1. Some things to consider.

      - Are there more than 5 scores in the Database?

      - Maybe Construct2 is not deecting the Text boxes? Delete all the Text boxes and add them again.

      2. In the PHP file getrank.php it adds +1. Find this line:

      // Print the rank (the number of people who have better score than you + 1)

      echo ($i + 1);

      Then delete the +1 part.

      Hope it helps fix the issues! :)

      • Thank you for replying! Rank was still out using the advice above but fixed it another way (I hope).

        I've definitely got more than 5 scores in the database. It's updating the leaderboard order OK when I submit a new result but still nothing after row 5 (just 0 0). Have tried twice to delete and re-add text boxes (do I need to set/add anything else in the layout? It is hard to tell from your example file - there are lots of additional textScore & textNames but assume that is just for your version? Couldn't see that they were referenced anywhere within the project). Is there anything else you can think of that might be affecting it and need changing? It's so odd.

        • I figured it out, for some reason, the array size is set to only have 10 items in it, you need to make it bigger before you start adding things to it.

          Hope this helps!

          • So in getscores.php I have changed it to this so I get 12 lines of results??:

            // Retrieve data from database

            $sql = "SELECT *

            FROM scores

            ORDER BY score

            LIMIT 24"; // The 'LIMIT 24' part will only read 24 scores. Feel free to change this value

            $result = mysqli_query($con, $sql);

            I currently have 10 scores in my database but my leaderboard is still only showing 5 scores. Is there anything else I need to change in php files or in Construct to adjust the array size?

            Thank you for looking into this for me. It has nothing to do with the initial values being 0 does it? and since 0 is a faster 'time' than other results in my database that it is showing these instead? Although the 0s are not being ordered at the top of the leaderboard so I'm guessing that isn't what's happening. The returned rank IS taking into account all the database records so I still haven't a clue.

  • Load more comments (70 replies)