How can PHP be used to calculate and display rankings based on points in a MySQL database?

To calculate and display rankings based on points in a MySQL database using PHP, you can retrieve the points from the database, sort them in descending order, assign rankings based on the sorted points, and then display the rankings alongside the corresponding points.

<?php
// Connect to MySQL database
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');

// Retrieve points from the database
$query = "SELECT user_id, points FROM users";
$result = $mysqli->query($query);

// Store points in an array
$points = array();
while ($row = $result->fetch_assoc()) {
    $points[$row['user_id']] = $row['points'];
}

// Sort points in descending order
arsort($points);

// Assign rankings based on sorted points
$rank = 1;
foreach ($points as $user_id => $point) {
    echo "Rank: " . $rank . " - User ID: " . $user_id . " - Points: " . $point . "<br>";
    $rank++;
}

// Close database connection
$mysqli->close();
?>