What is the best practice for generating a counter in a PHP script that stops when all database entries are outputted?

When generating a counter in a PHP script that stops when all database entries are outputted, you can achieve this by fetching all the database entries and counting them. Then, you can use a loop to iterate through the entries while incrementing a counter variable. The loop can continue until the counter reaches the total number of entries, at which point it will stop.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch all database entries
$result = $conn->query("SELECT * FROM your_table");
$total_entries = $result->num_rows;

// Counter variable
$counter = 0;

// Loop through entries
while ($row = $result->fetch_assoc()) {
    // Output entry or perform desired operation
    echo $row['column_name'] . "<br>";
    
    // Increment counter
    $counter++;
    
    // Check if all entries have been outputted
    if ($counter == $total_entries) {
        break; // Exit the loop
    }
}

// Close database connection
$conn->close();