Open In App

How to make a leaderboard using PHP ?

Last Updated : 28 Dec, 2020
Like Article

The purpose of this article is to make a simple program to create a leaderboard using PHP. Below is the implementation for the same using PHP.  The prerequisites of this topic are PHP/MySQL and the installment of Apache Server on your computer.


Step 1: First we will create a HTML table using <table> tag defined by the <tr> tag for creating rows. It’s just a simple HTML/CSS file. The only thing worth noting is that we are using the Bootstrap 4 CSS framework to give style to our page. You can use any other styling framework of your choice or write your own CSS if you want.


<!DOCTYPE html>
        <title>LeaderBoard Using PHP</title>
        <h2>Welcome To GFG</h2>

Step 2: Fetch data from SQL and display it using PHP.

Step 3: Create a MySQL database by following commands.

CREATE DATABASE leaderboard;
CREATE TABLE leaderboard(userName VARCHAR(30), marks INT(10));

Step 4: Insert some values into the database.

INSERT INTO leaderboard(userName,marks) VALUES('Devesh Pratap Singh',100);


/* Connection Variable ("Servername",
"username","password","database") */
$con = mysqli_connect("localhost"
        "root", "", "leaderboard");
/* Mysqli query to fetch rows 
in descending order of marks */
$result = mysqli_query($con, "SELECT userName, 
marks FROM leaderboard ORDER BY marks DESC");
/* First rank will be 1 and 
    second be 2 and so on */
$ranking = 1;
/* Fetch Rows from the SQL query */
if (mysqli_num_rows($result)) {
    while ($row = mysqli_fetch_array($result)) {
        echo "<td>{$ranking}</td>

Step 5: Close the table tag, body tag, and html tag. 



Step 6: Save this file as “index.php“.

Step 7: Copy this file inside your xampp server in htdocs directory.

Step 8: Open phpMyAdmin or MySQL server on your computer.

Step 9: Open https://localhost/index.php into your web browser.

Output after execution:

Output produced after running

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads