gpt4 book ai didi

php - 仅当 mysql 中不存在时才显示

转载 作者:行者123 更新时间:2023-11-29 23:37:57 25 4
gpt4 key购买 nike

我有一张 table ,上面有滑雪者和他们滑雪的滑雪站。

例如:

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

If I have three stations I want to show a text that say that the skier wasn't in this station.

I have:

 $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

最佳答案

问题是在每次迭代中,有两个 if 语句将为 false,并转到 else 语句。如果您建立一个包含所有可能性的数组,并删除他们去过的车站,最后您将获得他们未去过的车站列表。

此外,您确实不应该使用 mysql_query 函数,因为它们已被弃用,并将很快从 PHP 中删除。查看mysqli 。另外,对您的查询进行 SQL 注入(inject)。为了解决您的问题,我没有进行这些更改。

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

关于php - 仅当 mysql 中不存在时才显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com