如何使用PDO实现查询获取指定艺术家的全部专辑
解决思路与代码修正
1. 核心问题分析
你当前的代码存在两个关键问题:一是初始查询未按艺术家分组,导致每个卡片对应单张专辑而非单个艺术家;二是缺少根据指定艺术家查询其所有专辑的PDO逻辑。下面给出两种可行的解决方案:
2. 方案一:按需查询(分两次数据库请求)
先获取所有唯一艺术家,再在循环中查询每个艺术家的专辑,适合数据量较大的场景:
<?php // 第一步:获取所有唯一艺术家 $artistsStmt = $mysqlConnection->prepare('SELECT DISTINCT Artiste FROM album'); $artistsStmt->execute(); $artists = $artistsStmt->fetchAll(PDO::FETCH_COLUMN); // 仅提取艺术家名字列 ?> <?php foreach ($artists as $artistName) { ?> <!-- 艺术家卡片:点击触发对应模态框 --> <div class="music-card" data-bs-toggle="modal" data-bs-target="#artist-<?= htmlspecialchars($artistName) ?>"> <?php // 获取该艺术家的首张专辑封面 $coverStmt = $mysqlConnection->prepare('SELECT Artiste, Album FROM album WHERE Artiste = ? LIMIT 1'); $coverStmt->execute([$artistName]); $coverAlbum = $coverStmt->fetch(); ?> <a href="#"><img src="img/<?= htmlspecialchars($coverAlbum['Artiste'] . '_' . $coverAlbum['Album']) ?>.jpg" alt="<?= htmlspecialchars($artistName) ?>"></a> <p><?= htmlspecialchars($artistName) ?></p> </div> <!-- 艺术家专属模态框 --> <div class="modal fade" id="artist-<?= htmlspecialchars($artistName) ?>" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-xl"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Album de <?= htmlspecialchars($artistName) ?></h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="container-fluid"> <div class="row"> <?php // 查询该艺术家的所有专辑 $albumsStmt = $mysqlConnection->prepare('SELECT * FROM album WHERE Artiste = ?'); $albumsStmt->execute([$artistName]); $albums = $albumsStmt->fetchAll(); foreach ($albums as $album) { ?> <div class="col-md-4 mb-4"> <div class="card-left"> <div class="header-card"> <div class="proposition"> <div class="d-flex flex-row"> <div class="d-flex flex-column"> <a href="#"> <img class="img-fluid" src="img/<?= htmlspecialchars($album['Artiste'] . '_' . $album['Album']) ?>.jpg" alt="<?= htmlspecialchars($album['Album']) ?>"> </a> <div class="text-propo d-flex flex-column justify-content-center"> <p><?= htmlspecialchars($album['Album']) ?></p> <p><?= htmlspecialchars($album['Annee'] ?? '2019') ?></p> <!-- 假设年份字段为Annee,无数据则显示2019 --> </div> </div> </div> </div> </div> </div> </div> <?php } ?> </div> </div> </div> </div> </div> </div> <?php } ?>
3. 方案二:一次性查询+PHP分组(减少数据库请求)
如果数据量不大,可一次性获取所有专辑,再通过PHP按艺术家分组,提升性能:
<?php // 一次性获取所有专辑数据 $allAlbumsStmt = $mysqlConnection->prepare('SELECT * FROM album'); $allAlbumsStmt->execute(); $allAlbums = $allAlbumsStmt->fetchAll(); // 按艺术家分组整理数据 $artistsAlbums = []; foreach ($allAlbums as $album) { $artistName = $album['Artiste']; if (!isset($artistsAlbums[$artistName])) { $artistsAlbums[$artistName] = []; } $artistsAlbums[$artistName][] = $album; } ?> <?php foreach ($artistsAlbums as $artistName => $albums) { ?> <!-- 艺术家卡片 --> <div class="music-card" data-bs-toggle="modal" data-bs-target="#artist-<?= htmlspecialchars($artistName) ?>"> <?php $firstAlbum = $albums[0]; ?> <a href="#"><img src="img/<?= htmlspecialchars($firstAlbum['Artiste'] . '_' . $firstAlbum['Album']) ?>.jpg" alt="<?= htmlspecialchars($artistName) ?>"></a> <p><?= htmlspecialchars($artistName) ?></p> </div> <!-- 模态框 --> <div class="modal fade" id="artist-<?= htmlspecialchars($artistName) ?>" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-xl"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Album de <?= htmlspecialchars($artistName) ?></h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="container-fluid"> <div class="row"> <?php foreach ($albums as $album) { ?> <div class="col-md-4 mb-4"> <div class="card-left"> <div class="header-card"> <div class="proposition"> <div class="d-flex flex-row"> <div class="d-flex flex-column"> <a href="#"> <img class="img-fluid" src="img/<?= htmlspecialchars($album['Artiste'] . '_' . $album['Album']) ?>.jpg" alt="<?= htmlspecialchars($album['Album']) ?>"> </a> <div class="text-propo d-flex flex-column justify-content-center"> <p><?= htmlspecialchars($album['Album']) ?></p> <p><?= htmlspecialchars($album['Annee'] ?? '2019') ?></p> </div> </div> </div> </div> </div> </div> </div> <?php } ?> </div> </div> </div> </div> </div> </div> <?php } ?>
4. 关键注意事项
- SQL注入防护:始终使用PDO预处理语句(
prepare+execute),通过参数绑定传递变量,禁止直接拼接SQL字符串。 - XSS防护:用
htmlspecialchars()转义所有输出到HTML的变量,避免跨站脚本攻击。 - 模态框ID唯一性:用
artist-前缀+艺术家名字生成ID,避免特殊字符导致的DOM元素冲突。 - 语法修正:原代码存在
<?=$album['Artiste'](缺少闭合标签)、<?=$artist['album']r(多余字符)等语法错误,需及时修正。
内容的提问来源于stack exchange,提问作者Bruhat Melvin
相关产品推荐
相关产品推荐

