How can the LIMIT clause in SQL be utilized in conjunction with PHP for forum post retrieval?

When retrieving forum posts from a database using SQL in conjunction with PHP, the LIMIT clause can be used to limit the number of posts returned in each query. This can help improve performance by reducing the amount of data fetched from the database at once. To implement this, you can dynamically adjust the LIMIT clause based on the page number and the number of posts to display per page.

// Set the number of posts to display per page
$postsPerPage = 10;

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

// Construct the SQL query with the LIMIT clause
$sql = "SELECT * FROM forum_posts ORDER BY post_date DESC LIMIT $offset, $postsPerPage";

// Execute the query and fetch the results
$result = mysqli_query($conn, $sql);

// Loop through the results and display the forum posts
while ($row = mysqli_fetch_assoc($result)) {
    // Display post content
}