atxpunkrock
atxpunkrock

Reputation: 514

Mysql Union in PHP not returning correct results

I currently have a mysql Union query that works when placed directly into mysql via the command line, PHP MyAdmin or run in a Mysql editor like Mysql Workbench. When I run the query through my PHP class it returns two duplicate rows.

The Query:

SELECT `n`.`draftrd`, `n`.`draftteam`, `n`.`nflteam`, `nt`.`logo`, 
    `nt`.`name`, `nt`.`nflcity`,
    `p`.`class`, `p`.`first_name`, `p`.`last_name`
FROM `players` as `p` 
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`nflteam` = `nt`.`nflid` 
WHERE `n`.`playerid` = 2007000001
UNION
SELECT `n`.`draftrd` as `draftRd`, `n`.`draftteam` as `draftTm`, `n`.`nflteam`, 
    `nt`.`logo`, 
    `nt`.`name` as `draftName`, `nt`.`nflcity` as `draftCity`, `p`.`class`, 
    `p`.`first_name`, `p`.`last_name`
FROM `players` as `p` 
LEFT JOIN `nfl` as `n` ON `p`.`playerid` = `n`.`playerid`
LEFT JOIN `nflteams` as `nt` ON `n`.`draftteam` = `nt`.`nflid` 
WHERE `n`.`playerid` = 2007000001

In the editor it returns:

+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| draftrd | draftteam | nflteam | logo        | name    | nflcity  | class | first_name | last_name | tableid |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| 2       | 2         | 12      | giants.png  | Giants  | New York | 2007  | Martellus  | Bennett   |       1 |
| 2       | 2         | 12      | cowboys.png | Cowboys | Dallas   | 2007  | Martellus  | Bennett   |       1 |
+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
2 rows in set (0.00 sec)

The PHP return is: (The var_dump version)

array(18) { 
    [0]=> string(1) "2" 
    ["draftrd"]=> string(1) "2" 
    [1]=> string(1) "2" 
    ["draftteam"]=> string(1) "2"
    [2]=> string(2) "12" 
    ["nflteam"]=> string(2) "12" 
    [3]=> string(10) "giants.png" 
    ["logo"]=> string(10) "giants.png" 
    [4]=> string(6) "Giants" 
    ["name"]=> string(6) "Giants" 
    [5]=> string(8) "New York" 
    ["nflcity"]=> string(8) "New York" 
    [6]=> string(4) "2007" 
    ["class"]=> string(4) "2007"
    [7]=> string(9) "Martellus"
    ["first_name"]=> string(9) "Martellus"
    [8]=> string(7) "Bennett" 
    ["last_name"]=> string(7) "Bennett" 
}

I'm not sure what the issue is and why it's not working properly. I tried creating a temporary table and storing the query in it but it still gives me the duplicated rows. Is there an easier way to do this or an explanation of why it's happening? I've dumped the query in PHP to see if the issue is with it's construction but the dumped query returns the rows I'm looking for when run on the command line.

Upvotes: 0

Views: 323

Answers (1)

Chris
Chris

Reputation: 3338

The MySQL handles in PHP allow you to iterate through the result array in different ways. For example, you can choose to iterate through an associative array by key (e.g.: echo $result['draftteam'];), or you can iterate by index through a normal array (e.g.: echo $result[2];).

When you use the fetch_array() command, you are, by default, fetching both types at the same time. This would produce the arary that you provided.

If you are trying to create a dynamic table based off of the query (i.e.: one that writes each column name in a <th> before emptying it's contents into the <td>s below), then you should use the fetch_assoc() command, rather than the fetch_array() command. This will only return the text keys in your array.

while($arr = mysqli_fetch_assoc($sql)) {
    foreach($arr as $key => $value){
        echo $key .' => '. $value .'<br />'."\n";
    }
}

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://php.net/manual/en/mysqli-result.fetch-array.php

Upvotes: 1

Related Questions