What best practices should be followed when preparing a paging query in PHP?

When preparing a paging query in PHP, it is important to limit the number of records fetched from the database to improve performance and reduce load times. This can be achieved by using the LIMIT clause in SQL queries along with calculating the offset based on the current page number and the number of records per page.

// Calculate the offset based on the current page number and the number of records per page
$recordsPerPage = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $recordsPerPage;

// Prepare and execute the SQL query with LIMIT and OFFSET
$query = "SELECT * FROM table_name LIMIT $recordsPerPage OFFSET $offset";
$result = mysqli_query($conn, $query);

// Fetch and display the results
while($row = mysqli_fetch_assoc($result)) {
    // Display the data
}