PDA

View Full Version : Join tables into one Array


ptmuldoon
02-05-2008, 05:32 PM
I'm trying to join two tables that have a common field of 'gid' and to place the results of both tables into an array. And I'm not sure if I have the query wrong, or if I am creating the array incorrectly.

Am I going about it the correct way?


//Get the Player info
$sql2 = "SELECT * FROM game_players AS PINFO UNION SELECT * FROM game_info AS GINFO";
$query2 = mysql_query($sql2);
while ($row = mysql_fetch_assoc($query2)){
$pdata[] = $row;
}

angst
02-05-2008, 05:47 PM
this will do it:


$result = mysql_query("SELECT * FROM game_players gp, game_info gi WHERE gp.gid = gi.gid");
$MyArray = mysql_fetch_array($result);

ptmuldoon
02-05-2008, 07:48 PM
Thanks, but what if you don't want to query all the columns in the table, but to get specific, column_1, column_2 from TABLE 1 and column_3, column_5 from TABLE 2?

angst
02-05-2008, 07:52 PM
simple.
just include the fields names in the select portion, like:

$result = mysql_query("SELECT gp.SomeField, gi.SomeOtherField FROM game_players gp, game_info gi WHERE gp.gid = gi.gid");
$MyArray = mysql_fetch_array($result);


and so on.