What is the best practice for retrieving and displaying large amounts of SQL data in PHP?

When retrieving and displaying large amounts of SQL data in PHP, it is best to use pagination to limit the number of records fetched at a time. This helps improve performance by reducing the amount of data processed and displayed on each page load. By implementing pagination, you can enhance the user experience and prevent overwhelming the server with a large dataset.

<?php
// Set the number of records to display per page
$records_per_page = 10;

// Calculate the offset based on the current page number
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $records_per_page;

// Query to retrieve data with pagination
$sql = "SELECT * FROM your_table LIMIT $offset, $records_per_page";
$result = mysqli_query($conn, $sql);

// Display the data retrieved
while($row = mysqli_fetch_assoc($result)) {
    // Display each record here
}

// Pagination links
$total_records = // Query to get total number of records
$total_pages = ceil($total_records / $records_per_page);

for ($i = 1; $i <= $total_pages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
?>