【问题标题】:Mysql Union in PHP not returning correct resultsPHP中的Mysql Union没有返回正确的结果
【发布时间】:2012-12-03 20:26:39
【问题描述】:

我目前有一个 mysql Union 查询,当通过命令行、PHP MyAdmin 直接放入 mysql 或在 Mysql 编辑器(如 Mysql Workbench)中运行时,它可以工作。当我通过我的 PHP 类运行查询时,它返回两个重复的行。

查询:

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

在编辑器中返回:

+---------+-----------+---------+-------------+---------+----------+-------+------------+-----------+---------+
| 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)

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" 
}

我不确定问题是什么以及为什么它不能正常工作。我尝试创建一个临时表并将查询存储在其中,但它仍然给了我重复的行。有没有更简单的方法来做到这一点或解释为什么会发生这种情况?我已经在 PHP 中转储了查询,以查看问题是否与它的构造有关,但是转储的查询在命令行上运行时返回了我正在寻找的行。

【问题讨论】:

  • 请重新格式化...
  • 是的,第一次运行肯定很丑。哈哈
  • 我不明白你所说的“它是重复的”,如果你的意思是你有 [3]=> string(10) "giants.png" 和 ["logo"] => string(10) "giants.png" 这是因为您可以使用数字索引(例如 result[3])或文本索引(例如 result["logo"])访问查询结果跨度>
  • 你在var_dump() 上做了什么?另外,您是否对两者使用相同的 SQL 查询?我注意到手动示例包含tableid,但 PHP 没有。
  • 添加这些 tableid 列是为了尝试与其他开发人员一起解决问题。它们没有在实际的 PHP 类中使用。我删除了它们以防止进一步混淆。

标签: php mysql union


【解决方案1】:

PHP 中的 MySQL 句柄允许您以不同的方式遍历结果数组。例如,您可以选择按键遍历关联数组(例如:echo $result['draftteam'];),也可以按索引遍历普通数组(例如:echo $result[2];)。

当您使用fetch_array() 命令时,默认情况下,您会同时获取两种类型。这将生成您提供的数组。

如果您尝试根据查询创建动态表(即:在将每个列名写入<th> 之前将其内容清空到下面的<td>s 中),那么您应该使用@ 987654328@ 命令,而不是 fetch_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

【讨论】:

  • 谢谢。它消除了我对返回数组的一些误解。我知道这两种类型都有,但没有意识到它会为每个查询返回两种类型,除非指定。虽然它并没有完全回答我的问题,但它确实引导了我。我有一个函数返回 mysql_fetch_array 而不是 mysql_query 所以它只返回第一个结果。我返回查询,它为我提供了我正在寻找的所有信息。我必须研究返回此信息的最佳方式,因为我确信返回 mysql_query 不是最佳选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2012-08-01
  • 2014-05-10
  • 1970-01-01
相关资源
最近更新 更多