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

标签 php mysql

我的 SQL 数据库中有两个关联的表。一个是名为“airports”的机场列表,另一个是名为“airport_profiles”的航空公司列表。后者是我想重点关注的。我通过 airport_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 的分号分隔。任何建议都将受到极大的欢迎。 非常感谢。

最佳答案

查询更改为以下内容:

    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;

关于PHP - 关联表的爆炸结果查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57769364/

相关文章:

PHP $_SESSION 变量不会取消设置

php - 如何在 codeigniter 中写入新文件?

php - htaccess 对未找到但基于 url 中提供的特定目录的文件进行条件重写

php - 如何防止通过 URL 直接下载和查看文件

php - Facebook 喜欢通知系统

mysql - 从子表中选择多行数据以及MySQL中的父行表

ftp 服务器上的 Java Applet 需要几分钟才能连接到 MySQL 数据库

python-mysqldb插入图像int值错误

php - 如何返回数组中所有值都存在的行

php - 注册 session 执行 PDOStatement::execute(): SQLSTATE[HY093]: