2014-10-11 66 views
0

不存在我有滑雪者和滑雪站的表,他們在滑雪顯示只有在MySQL

例如:

 
--------------------- 
    skier  station 
--------------------- 
| 1  | 2 
| 1  | 3 
| 2  | 2 
--------------------- 

如果我有三個站我想顯示文本那說滑雪者不在這個地方。

我:

$result="SELECT * FROM skiers_and_stations WHERE skier = '$id_skier';"; 
$makeResult=mysql_query($result, $conexion); 
while ($row = mysql_fetch_array($makeResult, MYSQL_ASSOC)) { 
if ($row['station'] == '1') { } else {echo "No here before, station 1"}; 
if ($row['station'] == '2') { } else {echo "No here before, station 2"}; 
if ($row['station'] == '3') { } else {echo "No here before, station 3"}; 
} 

但它不工作。

我想顯示:

 
Skier 1 
No here before, station 1 

回答

0

的問題是在每次迭代兩個if語句是假的,然後轉到else語句。如果您建立了一個具有所有可能性的陣列,並刪除他們去過的電臺,最後您將獲得他們沒有去過的電臺列表。

此外,你真的不應該使用mysql_query函數,因爲它們已被棄用,並將很快從PHP中刪除。看看mysqli。此外,您的查詢SQL注入。爲了解決你的問題,我沒有做出這些改變。

$result = "SELECT * FROM skiers_and_stations WHERE skier = '$id_skier';"; 
$makeResult = mysql_query($result, $conexion); 
$stations = array(1 => true, 2 => true, 3 => true); 
while ($row = mysql_fetch_array($makeResult, MYSQL_ASSOC)) { 
    unset($stations[$row['station']]); 
} 
foreach ($stations as $stationId => $val) { 
    echo "Not here before, station $stationId"; 
}