What is the common issue with only the last value being output in a text file when using PHP to fetch data from a database?

The common issue with only the last value being output in a text file when using PHP to fetch data from a database is that the file is being overwritten with each iteration of the loop that fetches data from the database. To solve this issue, you can open the file in append mode instead of write mode, so that each fetched value is appended to the file rather than overwriting it.

<?php
// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");

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

// Fetch data from the database
$sql = "SELECT column_name FROM table_name";
$result = $connection->query($sql);

// Open the file in append mode
$file = fopen("output.txt", "a");

// Loop through the fetched data and write to the file
while($row = $result->fetch_assoc()) {
    fwrite($file, $row['column_name'] . "\n");
}

// Close the file and database connection
fclose($file);
$connection->close();
?>