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

33

Index

Attached Files

The following files have been attached to this tutorial:

.zip
.capx

highscore-example.capx

Download now 180.37 KB

Stats

43,750 visits, 115,263 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 26 Sep, 2015. Last updated 18 Dec, 2020

In this tutorial you will learn how to create your own Highscores for your games, using your own free server / database. Let's go!

Here's an example of how the leaderboard with high scores can look on your game. Image: Santa Hop!

What do you need?

- Your Construct 2 game (or C3)

- A server with PHP and MySQL databases (you can get a free server on 000webhost.com)

- Download the PHP7 files (phps.zip is for older versions of php5)

Let's start, step by step...

Registrating Your New Free Server

A server is like a computer on internet where you can save your own data: pictures, files, databases, scores, games and many more things...

000webhost.com offers free web hosting, with PHP and MySQL. It allows you to create 1 free website, host your files and you can create 2 free Databases.

Go to http://www.000webhost.com. Click "Sign Up" and register as a new user.

Fill your name, email and select a new password. When you are done you will receive a confirmation email, open your email account and Confirm your registration.

Back to 000webhost, select Create a New Site. Then type a name for your free domain, ie: duquekarlgames.000webhostapp.com

The next step is to access your dashboard.

Access your Dashboard

If you have already signed up and confirmed your free account, then you can log in to your account. To access your Dashboard, click Manage Your Website.

  • 112 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • 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.

  • [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.

  • Great tutorial for a refresher. Ive just finished reading through the tutorial and will soon be implementing the PHP.

  • I may be a bit late to the party, but I just finished this great tutorial by the letter, but the construct 3 part did not work. "work" means, no data was retreived after the ajax request. The phps work just fine, so I went bugfixing. as some users here seem to have similar problems, I want to share my solution:

    Instead of using a constant for the URL as suggested in the tutorial, i hardcoded it in the ajax request. this works like a charm. maybe it helps somebody else :-)

    thank you, dear author of this fine tutorial. well written and a huge help.

    • Hey! Thanks for writing and I am happy that you enjoyed the tutorial and it was helpful!! Go and make some super awesome games and share them here later! ;)

      • Hi, thanks fo the wonderful tutorial but I have an Issue, I hope you can help me by getting block issue in CORS

        how do I deal with it?

        how do I implement the code that allowing me to access in different site address?

  • Thankyou DuqueKarl for putting together this Tutorial. Very helpful

  • Load more comments (94 replies)