Reputation: 833
I have the following line:
$products = $dbh->query("SELECT piP2Components.typeID, invTypes.typeName FROM piP2Components INNER JOIN invTypes ON piP2Components.typeID = invTypes.typeID ORDER BY invTypes.typeName ASC;");
I have another table, piP3Components and I would like to run the same query on it and have the results added to the $products variable. As the result is a PDOStatement object I can't simply array_push.
How would I go about doing this? Alternatively, I'm fairly new to using JOIN queries, is there a way to accomplish this in SQL without having the piP3Components.typeID results in a different column?
Thanks.
Upvotes: 1
Views: 4788
Reputation: 51411
You have two options.
First, if the columns you're picking from each table have identical column types, you can use a UNION
:
SELECT foo, bar, baz FROM something WHERE ...
UNION ALL
SELECT qux AS foo, meta AS bar, syntactic AS baz FROM elsewhere WHERE ...
Second, you can run both queries, then fetch the results for each and place them in a single array, using that array later instead of the statement handle when processing the results:
$results = array();
$sth_a = $pdo->prepare(...);
$sth_a->execute(...);
while($row = $sth_a->fetch(PDO::FETCH_ASSOC))
$results[] = $row;
$sth_b = $pdo->prepare(...);
$sth_b->execute(...);
while($row = $sth_b->fetch(PDO::FETCH_ASSOC))
$results[] = $row;
print_r($results);
Upvotes: 9