How to make a leaderboard using PHP ?
Last Updated :
23 Jul, 2025
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.
Approach:
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.
HTML
<!DOCTYPE html>
<html>
<head>
<title>LeaderBoard Using PHP</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<table>
<tr>
<td>Ranking</td>
<td>UserName</td>
<td>Marks</td>
</tr>
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);
PHP
<?php
/* 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>
<td>{$row['userName']}</td>
<td>{$row['marks']}</td>";
$ranking++;
}
}
?>
Step 5: Close the table tag, body tag, and html tag.
HTML
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