MySQL控制台中的查询结果与PHP的mysql_query()的结果不匹配

时间:2010-09-20 17:09:20

标签: php mysql

我的MySQL查询会根据查询的提交方式返回不同的结果。当通过MySQL控制台提交查询结果时。

mysql> SELECT   `modx`.coverage_nation.id,
    -> `modx`.coverage_nation.name,
    -> `modx`.coverage_national_region.id,
    -> `modx`.coverage_national_region.name
    -> FROM             `modx`.coverage_nation_part
    -> RIGHT JOIN       `modx`.coverage_national_region ON (`modx`.coverage_nati
on_part.nation_regionID = `modx`.coverage_national_region.id)
    -> RIGHT JOIN       `modx`.coverage_nation ON (`modx`.coverage_nation_part.n
ationID = `modx`.coverage_nation.id)
    -> ORDER BY `modx`.coverage_nation.name ASC, `modx`.coverage_national_region
.name ASC;
+----+---------------+------+------+
| id | name          | id   | name |
+----+---------------+------+------+
|  3 | Canada        | NULL | NULL |
| 18 | Chad          | NULL | NULL |
| 17 | Germany       | NULL | NULL |
| 15 | Italy         | NULL | NULL |
|  2 | Mexico        | NULL | NULL |
| 19 | Nigeria       | NULL | NULL |
| 14 | Russia        | NULL | NULL |
| 16 | Spain         | NULL | NULL |
|  1 | United States | NULL | NULL |
+----+---------------+------+------+
9 rows in set (0.00 sec)

当使用PHP的mysql_query提交相同的查询时,它只返回一行。

$query .= "SELECT   `modx`.coverage_nation.id,
`modx`.coverage_nation.name,
`modx`.coverage_national_region.id,
`modx`.coverage_national_region.name
FROM        `modx`.coverage_nation_part
RIGHT JOIN  `modx`.coverage_national_region ON (`modx`.coverage_nation_part.nation_regionID = `modx`.coverage_national_region.id)
RIGHT JOIN  `modx`.coverage_nation ON (`modx`.coverage_nation_part.nationID = `modx`.coverage_nation.id)
ORDER BY `modx`.coverage_nation.name ASC, `modx`.coverage_national_region.name ASC;";

$resultSet = mysql_query($query) or die("query failed ".mysql_error());

while($row = mysql_fetch_array($resultSet,MYSQL_NUM)) {
    // handle each result here
}

仅返回加拿大。有没有人对如何解决这个问题有任何想法?

2 个答案:

答案 0 :(得分:0)

您有多个名称为idname的字段。尝试将SQL的SELECT部分​​(在PHP代码中)更改为:

SELECT `modx`.coverage_nation.id,
`modx`.coverage_nation.name,
`modx`.coverage_national_region.id AS coverage_national_region_id,
`modx`.coverage_national_region.name AS coverage_national_region_name

或类似的东西,所以返回的字段有不同的名称。

答案 1 :(得分:0)

我的代码中出现了错误。我坐下来重新编写它现在有效。