What are some best practices for efficiently retrieving and displaying MySQL data in PHP without using date-Seek?

When retrieving and displaying MySQL data in PHP without using date-Seek, it is important to optimize your queries and use appropriate indexing on your database tables. One way to efficiently retrieve data is to use LIMIT and OFFSET in your SQL queries to fetch data in smaller chunks rather than retrieving all data at once. Additionally, consider caching frequently accessed data to reduce the number of database queries.

<?php

// Establish a database connection
$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);
}

// Retrieve data using LIMIT and OFFSET
$limit = 10; // Number of rows to retrieve
$offset = 0; // Starting row index

$sql = "SELECT * FROM table_name LIMIT $limit OFFSET $offset";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>