so I am making image modal gallery and I have this issue which a can’t resolve. I want to fetch first image from database table. The issue is that I’m getting error Call to a member function on array error.
My function is this:
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();
}
And my HTML looks like this:
<?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; ?>
Error message:
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
Thank you very much for all your responses.
>Solution :
Re-using variable names for to represent different things in the same script is never a good idea – it makes your code harder to understand, which means it’s easy to get confused when writing it and testing it, and it’s very likely to cause subtle bugs.
It looks like it’s the cause of your problem here too. This code:
<?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; ?>
should be changed to:
<?php
$first_photo = new Photo;
foreach ($gallery as $galleryItem):
$first_photo_data = $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 $galleryItem['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 $galleryItem['nazov'];?>/<?php echo $first_photo_data['url'];?>">
</a>
</div>
<?php endforeach; ?>
I have renamed the variable which holds the return value of the fetch_first_photo function to $first_photo_data, so it is not overwriting the value of $first_photo.
Why? Well, in your original code it appears that $first_photo is defined ok to begin with, but the next time your loop runs, you’ve overwritten it with the result of the fetch_first_photo() function. As we can see from your code that function returns an array, so you cannot then call a function on it, and this would lead to the error you quoted.
N.B. Although not directly the cause of the issue, I also renamed the item variable in the foreach, for the same reason: having good, clear, unambiguous code.