How can you display records between a specific range in a PHP query?
To display records between a specific range in a PHP query, you can use the LIMIT clause in your SQL query. The LIMIT clause allows you to specify the number of records to return and an optional offset to start from. By using LIMIT with appropriate values, you can display records within a specific range.
// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// Define the range
$offset = 0; // Starting record
$limit = 10; // Number of records to display
// Query to select records within the specific range
$sql = "SELECT * FROM your_table LIMIT $offset, $limit";
// Execute the query
$result = $conn->query($sql);
// Display the records
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();