简体   繁体   English

PHP中的Mysql Union未返回正确结果

[英]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. 我目前有一个mysql Union查询,当通过命令行,PHP MyAdmin直接放置在mysql中或在Mysql编辑器(如Mysql Workbench)中运行时,该查询可以工作。 When I run the query through my PHP class it returns two duplicate rows. 当我通过PHP类运行查询时,它将返回两个重复的行。

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) PHP返回的是:(var_dump版本)

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. 我已经在PHP中转储了查询,以查看问题是否出在结构上,但是转储的查询返回了在命令行上运行时要查找的行。

The MySQL handles in PHP allow you to iterate through the result array in different ways. PHP中的MySQL句柄允许您以不同的方式遍历结果数组。 For example, you can choose to iterate through an associative array by key (eg: echo $result['draftteam']; ), or you can iterate by index through a normal array (eg: echo $result[2]; ). 例如,您可以选择通过键迭代关联数组(例如: echo $result['draftteam']; ),也可以选择通过索引遍历普通数组(例如: echo $result[2]; )。

When you use the fetch_array() command, you are, by default, fetching both types at the same time. 当您使用fetch_array()命令时,默认情况下,您是同时获取两种类型的。 This would produce the arary that you provided. 这将产生您提供的arary。

If you are trying to create a dynamic table based off of the query (ie: 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. 如果您尝试基于查询创建动态表(即:在将其内容清空到下面的<td>之前在<th>中写入每个列名称的表),则应使用fetch_assoc()命令,而不是fetch_array()命令。 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-assoc.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM