What potential issues can arise when generating a list of top scorers from a database table using PHP?

One potential issue that can arise when generating a list of top scorers from a database table using PHP is that the query may not be optimized for performance, especially if the table is large. To solve this issue, you can use SQL functions like LIMIT to retrieve only the necessary data and ORDER BY to sort the results efficiently.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to get top scorers
$sql = "SELECT player_name, score FROM players ORDER BY score DESC LIMIT 10";
$result = $conn->query($sql);

// Display the list of top scorers
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Player: " . $row["player_name"]. " - Score: " . $row["score"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();