How can you avoid double output in PHP MySQL query results?
When fetching data from a MySQL database using PHP, you may encounter double output in query results if you are not handling the data properly. To avoid this issue, you can use the `mysqli_fetch_assoc()` function to fetch each row as an associative array, and then loop through the results to display them without duplication.
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query to fetch data
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Fetch and display data without double output
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column1'] . " - " . $row['column2'] . "<br>";
}
// Close database connection
mysqli_close($connection);