代码之家  ›  专栏  ›  技术社区  ›  Brook Julias

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

  •  1
  • Brook Julias  · 技术社区  · 14 年前

    我的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 回复  |  直到 14 年前
        1
  •  0
  •   webbiedave    14 年前

    您有多个具有名称的字段 id name . 尝试将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
    

    或者类似的返回字段有不同的名称。

        2
  •  0
  •   Brook Julias    14 年前

    我的代码有错误。我坐下来重新写了一遍,现在开始工作了。