Put multiple querys in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lammspillning
Forum Newbie
Posts: 14
Joined: Fri Aug 17, 2007 10:50 am

Put multiple querys in array

Post by lammspillning »

How would I go on to do these mysql_querys

mysql_query("SELECT * FROM my_table_1");
mysql_query("SELECT * FROM my_table_2");
mysql_query("SELECT * FROM my_table_3");
mysql_query("SELECT * FROM my_table_4");

..and put all the results in a array and echo that array..?
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Put multiple querys in array

Post by JakeJ »

Assuming they all contain similar elements, try using a UNION query.

$qry = mysql_query("(SELECT * FROM table1) UNION (SELECT * FROM table2) UNION (SELECT * FROM table3)");
$results = mysql_fetch_array($qry);

Then you have your array!

***Here's another thought: I don't know what your data structure is like, but if you have multiple tables with similar data, I suspect it's not normalized properly. It might all belong in one table.
Post Reply