1

I am having problems displaying images which have been queried from the database. The array from the query result is looped through and the image name is extracted, then passed to a variable which also contains the image path. However when the variable is included in the output, a torn image icon is displayed (no image).

Code:

public function loadImages($item) {
  $type = $item;
  $conn = $this->create_connection('read');
  $sql = "SELECT * FROM `items` WHERE catagory='$type'";     
  $result = $conn->query($sql)or die(mysql_error());    
  while($row = mysqli_fetch_array($result)) {
     $path = '../images/thumbs/'.$row['image_path'];
     //var_dump($path);
     echo '<li><img src="../images/thumbs/'.$path.'"/></li>';
  }

When I var_dump $path, it displays the correct file path: string(28) "../images/thumbs/image1.jpeg", with a further seven items, so I am not losing data. I can confirm the path is correct, when I echo the full path and image name, the image displays correctly.

echo '<li><img src="../images/thumbs/image1.jpg"/></li>';

Also I have tried removing the $path variable and applying $row directly:

echo '<li><img src="../images/thumbs/'.$row['image_path'].'"/></li>';

I thought the problem was with joining the two strings, so I tried replacing the image variable with $full_path, which also did not work:

 $full_path = $path . $row['image_path'];

$row is correct because when I do a var-dump for $row['image_path'] I get the following:

string(11) "image1.jpeg" string(11) "image2.jpeg" string(11) .....

I previously had the full image path held within the database, when the full image path was used, the images displayed correctly. I later decided only to store the image name in the database, as the path could change in the future, so the design change created this problem.

4
  • What is the img tag that's actually emitted to the browser? How does it different from the img tag that you manually echoed which worked?
    – David
    Nov 18, 2011 at 18:21
  • the only difference I can see is adding the varible to the string I don't think I am using the correct syntax?? ../images/thumbs/image1.jpg (manual) ../images/thumbs/'.$path.' (adding varible)
    – Alan
    Nov 18, 2011 at 18:32
  • Right, but what is the result of the string concatenation? If the images are known to be available and the browser isn't rendering them then the img tag has a problem. Don't just look at the broken image, look at the HTML source of the page and compare the img tag with what you think it should be. That will tell you exactly what's wrong.
    – David
    Nov 18, 2011 at 18:36
  • I've done a stupid thing been working on this too long today! The file extension is incorrect in my database (jpeg), should be (jpg). Thanks for all your help!!
    – Alan
    Nov 18, 2011 at 19:05

3 Answers 3

3
while($row = mysqli_fetch_array($result)) {
     $path = '../images/thumbs/'.$row['image_path'];
     //var_dump($path);
     echo '<li><img src="../images/thumbs/'.$path.'"/></li>';
}

$path already contains "../images/thumbs/", but you add it again when echoing the line.

2
  • thanks, yes I originally just added $row['image_path] to $path and then echo out echo '<li><img src="../images/thumbs/'.$path.'"/></li>'; - but i have tried many different ways to get this to work, what would you suggest would be the best approach?
    – Alan
    Nov 18, 2011 at 18:29
  • I've done a stupid thing been working on this too long today! The file extension is incorrect in my database (jpeg), should be (jpg). Thanks for all your help!!
    – Alan
    Nov 18, 2011 at 19:10
1

You have the path twice on your echo.

Your code should be like this:

while($row = mysqli_fetch_array($result)) {
    $path = '../images/thumbs/'.$row['image_path'];
    //var_dump($path);
    echo '<li><img src="$path"/></li>';
}

If it doesn't work, try to copy that var_dump result and paste in your browser to check if the path is right.

5
  • ok I have added the path and file name to path: $path = '../images/thumbs/'.$row['image_path']; and then i have echo this out: echo '<li><img src="'.$path.'"/></li>'; images still do not display??? var dump of $path produces correct path as used before: string(28) "../images/thumbs/image2.jpeg"
    – Alan
    Nov 18, 2011 at 18:44
  • path is correct if i comment it out and replace with echo '<li><img src="../images/thumbs/image1.jpg"/></li>'; the image displays correctly
    – Alan
    Nov 18, 2011 at 18:47
  • 1
    does the image shows up when you paste the path on your browser address?
    – v42
    Nov 18, 2011 at 18:53
  • I've done a stupid thing been working on this too long today! The file extension is incorrect in my database (jpeg), should be (jpg). Thanks for all your help!!
    – Alan
    Nov 18, 2011 at 19:01
  • Heh, that happens! I told you to check the path just because I did it a few times already. :p
    – v42
    Nov 18, 2011 at 19:04
1

../images/thumbs/ is being echoed twice. Try this:

echo '<li><img src="'.$path.'"/></li>';
3
  • ok I have added the path and file name to path: $path = '../images/thumbs/'.$row['image_path']; and then i have echo this out: echo '<li><img src="'.$path.'"/></li>'; images still do not display???
    – Alan
    Nov 18, 2011 at 18:43
  • path is correct if i comment it out and replace with echo '<li><img src="../images/thumbs/image1.jpg"/></li>'; the image displays correctly
    – Alan
    Nov 18, 2011 at 18:47
  • I've done a stupid thing been working on this too long today! The file extension is incorrect in my database (jpeg), should be (jpg). Thanks for all your help!!
    – Alan
    Nov 18, 2011 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.