2014-10-11 16 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"; 
}