What are the potential pitfalls of using subselects in PHP for complex database queries?

Using subselects in PHP for complex database queries can lead to performance issues, as each subselect query will be executed separately. To avoid this, it is recommended to use JOINs or other methods to combine multiple queries into a single query for better performance.

// Example of using JOIN instead of subselects for a complex query
$query = "SELECT * FROM table1 
          JOIN table2 ON table1.id = table2.table1_id 
          WHERE table1.column = 'value'";

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

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