You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL模态图片画廊报错:Call to a member function on array

问题:获取MySQL图片模态画廊首张图片时出现「Call to a member function on array」错误

我正在开发基于MySQL的图片模态画廊,需要从数据库表中获取首张图片,但出现了「Call to a member function on array」错误,请求协助解决。

函数代码

public function fetch_first_photo($id){
  global $pdo;
  $query = $pdo->prepare('SELECT * FROM photo WHERE id_galeria = ? LIMIT 1');
  $query->execute([$id]);
  return $query->fetch();
}

HTML/PHP代码

<?php 
$first_photo = new Photo;

foreach ($gallery as $gallery):
  $first_photo = $first_photo->fetch_first_photo($gallery['id_galeria']); ?>
  <div class="col"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" viewBox="0 0 16 16" class="bi bi-pin-map" style="display: inline;padding-bottom: 1px;margin-bottom: 10px;"></svg>
    <h3 class="text-center" style="width: 80%;display: inline;margin-left: 10px;"><?php echo $gallery['nazov'] ?></h3><a data-bs-target="#modal-1<?php echo $i ?>" data-bs-toggle="modal" href="#">
    <img class="img-fluid gallery" data-bss-hover-animate="pulse" src="assets/img/Referencie/<?php echo $gallery['nazov'];?>/<?php echo $first_photo['url'];?>">
   </a>
  </div>
 <?php endforeach; ?>

报错信息

Fatal error: Uncaught Error: Call to a member function fetch_first_photo() on array in /data/8/b/8b8f5c3a-29b5-4e71-a68b-85bf5fa61ab2/falconi.eu/web/referencie2.php:30 Stack trace: #0 {main} thrown in/data/8/b/8b8f5c3a-29b5-4e71-a68b-85bf5fa61ab2/falconi.eu/web/referencie2.php on line 30


解决方案

核心问题

在foreach循环中,你将原本是Photo类实例的$first_photo变量,重新赋值为fetch_first_photo()方法的返回值(该方法返回数据库查询结果数组,无结果时返回false)。第一次循环后,$first_photo不再是类实例,而是数组,第二次循环调用$first_photo->fetch_first_photo()时,就会触发「调用数组的成员函数」错误。

修复步骤

  1. 重命名接收结果的变量:保留类实例变量,用新变量存储每次查询的结果,同时优化循环变量名避免混淆:
<?php 
$photoInstance = new Photo;

foreach ($gallery as $galleryItem):
  $currentFirstPhoto = $photoInstance->fetch_first_photo($galleryItem['id_galeria']); ?>
  <div class="col"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" viewBox="0 0 16 16" class="bi bi-pin-map" style="display: inline;padding-bottom: 1px;margin-bottom: 10px;"></svg>
    <h3 class="text-center" style="width: 80%;display: inline;margin-left: 10px;"><?php echo $galleryItem['nazov'] ?></h3><a data-bs-target="#modal-1<?php echo $i ?>" data-bs-toggle="modal" href="#">
    <?php if ($currentFirstPhoto): ?>
    <img class="img-fluid gallery" data-bss-hover-animate="pulse" src="assets/img/Referencie/<?php echo $galleryItem['nazov'];?>/<?php echo $currentFirstPhoto['url'];?>">
    <?php else: ?>
    <!-- 无图片时显示默认图或提示 -->
    <img class="img-fluid gallery" data-bss-hover-animate="pulse" src="assets/img/default.jpg" alt="暂无图片">
    <?php endif; ?>
   </a>
  </div>
 <?php endforeach; ?>
  1. 额外优化建议:
    • 避免使用global $pdo,通过类构造函数注入PDO实例,提升代码可维护性:
      class Photo {
        private $pdo;
        public function __construct($pdo) {
          $this->pdo = $pdo;
        }
        public function fetch_first_photo($id){
          $query = $this->pdo->prepare('SELECT * FROM photo WHERE id_galeria = ? LIMIT 1');
          $query->execute([$id]);
          return $query->fetch();
        }
      }
      // 实例化时传入PDO
      $photoInstance = new Photo($pdo);
      

内容的提问来源于stack exchange,提问作者Richard Gaál

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 17:53:12