PHP-关联表的爆炸结果查询

时间:2019-09-03 10:00:32

标签: php mysql

我的sql数据库中有两个关联的表。一个是名为“机场”的机场列表,另一个是名为“ airport_profiles”的航空公司列表。后者是我想重点关注的。我通过airport_id行将航空公司与机场关联。即ID号引用了机场。我当前的PDO查询是:

if(isset($_GET['airport'])) {

if ($_GET['airport'] == 1) { 
   $airportQuery = "SELECT * FROM airport_profiles";
   $airport = $db->prepare($airportQuery);
   $airport->execute();
}
else {
    $airportQuery = "SELECT * FROM airport_profiles WHERE airport_id = :airport_id";
    $airport = $db->prepare($airportQuery);
    $airport->execute(['airport_id' => $_GET['airport']]);
}   
$selectedAirport = $airport->fetchAll(); 

foreach ($selectedAirport as $profile) {        
    if ($profile['id'] == 1) continue;

我尝试插入     $profile = explode(';', $airport_id); 但这根本不起作用。

如果我错了,请纠正我,但据我了解,为了将多个航空公司与一个机场联系起来,我需要添加一个爆炸“;”查询,以便我的airport_id的分号分开。任何建议都将受到欢迎。 非常感谢。

1 个答案:

答案 0 :(得分:0)

查询更改为以下内容:

    if(isset($_GET['airport'])) {
        if ($_GET['airport'] == 1) {
            $airportQuery = "SELECT * FROM airport_profiles GROUP BY airline_name";
            $airport = $db->prepare($airportQuery);
            $airport->execute();
        }
else {
        $airportQuery = "SELECT * FROM airport_profiles WHERE airport_id = :airport_id";
        $airport = $db->prepare($airportQuery);
        $airport->execute(['airport_id' => $_GET['airport']]);
    }

    $selectedAirport = $airport->fetchAll();

        foreach ($selectedAirport as $profile) {
        if ($profile['id'] == 1) continue;