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

44,259 visits, 116,039 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

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":

CREATE TABLE `scores` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR( 20 ) NOT NULL , `score` INT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB;

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

Great!

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

  • 112 Comments

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

  • thank you for the tutorial it was very helpful in my game development

  • Load more comments (103 replies)