What are the potential pitfalls of using UNION in MySQL queries for data transformation in PHP?

Using UNION in MySQL queries for data transformation in PHP can lead to performance issues as it requires the database to process multiple queries and combine their results. To avoid this, it is recommended to use JOINs or subqueries instead of UNION. This will help optimize the query and improve overall performance.

// Example of using JOIN instead of UNION in MySQL query for data transformation in PHP

$query = "SELECT table1.column1, table2.column2 
          FROM table1 
          JOIN table2 ON table1.id = table2.id 
          WHERE table1.column3 = 'value'";

$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        // Process data here
    }
} else {
    echo "No results found.";
}