What are some best practices for looping through and sending multiple rows of data in PHP?

When looping through and sending multiple rows of data in PHP, it is best practice to use prepared statements to prevent SQL injection attacks and improve performance. Additionally, it is recommended to fetch the data in batches to reduce memory usage and optimize processing speed.

// Assuming $conn is a valid database connection
$stmt = $conn->prepare("SELECT * FROM table_name");
$stmt->execute();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process each row of data here
    // Send the data or perform any necessary operations
}