2010-09-20 58 views
1

的結果不匹配我的MySQL查詢根據查詢的提交方式返回不同的結果。當查詢當同樣的查詢使用提交通過MySQL控制檯結果。MySQL控制檯中的查詢結果與PHP的mysql_query()

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 
} 

只返回加拿大。有沒有人有任何想法可以解決這個問題?

+0

相同的查詢通常會導致相同的結果。 (除非有非常奇怪的觀點和用戶權限在繼續)更有可能的是,PHP代碼處理它時有一個小錯誤,謹慎發佈? – Wrikken 2010-09-20 17:11:56

+0

如果您覺得有幫助,可以在https://docs.google.com/leaf?id=0B2rZ59SOyaKmOWIxZTlhMGItYjVmNi00MDRkLWFjZjctODE2NDM3YzkwMWFi&hl=en&authkey=CPqy7Y4D上找到完整代碼。查詢在第一個文件中提交,結果在第二個文件中處理。 https://docs.google.com/leaf?id=0B2rZ59SOyaKmZDZjYzI2Y2EtMmFiZS00NjQyLTliZWQtMWQ3Y2VkNWZhMDY5&hl=en&authkey=CP3S5_4C – 2010-09-21 14:14:01

+0

等待。您發佈的代碼與正在運行的代碼不同,而是與您認爲正在運行的代碼相加。 – webbiedave 2010-09-21 19:21:52

回答

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 

或者類似這樣返回的字段有不同的名稱。

+0

我正在使用'while($ row = mysql_fetch_array($ resultSet,MYSQL_NUM)){}'所以我正在處理一個數字而不是一個關鍵字符串。 – 2010-09-21 13:58:16

+0

我明白了。謝謝(你的)信息。 'mysql_num_rows($ resultSet);'返回什麼? – webbiedave 2010-09-21 14:56:27

+0

'mysql_num_rows($ resultSet)';返回1 – 2010-09-21 18:16:07