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

  • 44 favourites


Attached Files

The following files have been attached to this tutorial:



Download now 180.37 KB


24,446 visits, 66,327 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.

  • Bro... You're GOD for me right now, thank you so much!

  • Thank you so much for posting this! I got this to work with construct 3 and now im integrating the code with my custom name writing and text graphics. (Any thing system font is ugly on any device).

    Also a great way to learn interaction with construct 3 and a server.

    • Great! Yes, it should work in C3 as well.

      If you liked my tutorial, give it some love! A thumbs up or Favorite, so it can help others too ;)

  • Is there anyway for me to change the code so that is will give you the lowest scores as my game doesn't have a score but rather a speed run clock that goes up the longer you take to finish. So my "high scores" need to be the ones with the lowest values can you help me at all with where and what in the code I need to change?

    • Yes!

      Open 'getscores.php' and replace "ORDER BY score DESC" (scores are ordered DESCending - higher first then lower) with "ORDER BY score ASC" (scores will be ordered ASCending - lowest values first, then higher)

      // Retrieve data from database

      $sql = "SELECT *

      FROM scores

      ORDER BY score DESC // Replace 'DESC' with 'ASC'

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

  • Load more comments (48 replies)