views:

40

answers:

2

My MySQL query returns different results depending on how the query is submitted. When the query is submitted through the MySQL Console results in.

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)

When the same query is submitted using PHP's mysql_query it returns only one row.

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

Returns only Canada. Does anyone have any ideas as to how I might solve this?

A: 

You have more than one field with the names id and name. Try changing the SELECT portion of your SQL (in the PHP code) to:

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

Or something similar so returned fields have distinct names.

webbiedave
I am using `while($row = mysql_fetch_array($resultSet,MYSQL_NUM)) {}` so I am dealing with a number not a key string.
Brook Julias
I see. Thanks for the info. What does `mysql_num_rows($resultSet);` return?
webbiedave
`mysql_num_rows($resultSet)`; returns 1
Brook Julias
A: 

There was an error in my code somewhere. I sat down and rewrote it and it now works.

Brook Julias