How can the PHP code be optimized to prevent automatic querying of ranking data when the page is loaded?

To prevent automatic querying of ranking data when the page is loaded, you can implement caching mechanisms to store the ranking data and only query it periodically or when it is updated. This way, the ranking data is not queried every time the page is loaded, improving performance and reducing unnecessary database queries.

// Check if the ranking data is already cached
if(!($rankingData = apc_fetch('ranking_data'))) {
    // If not cached, query the ranking data from the database
    $rankingData = query_ranking_data_from_database();

    // Cache the ranking data for future use
    apc_store('ranking_data', $rankingData, 3600); // Cache for 1 hour
}

// Use the ranking data for display on the page
foreach($rankingData as $rank) {
    echo $rank['name'] . ': ' . $rank['score'] . '<br>';
}