user1944305
user1944305

Reputation: 153

Displaying an image in PHP using name stored in database

I am trying to display an image that is in a folder "upload" by getting the image name from the database.

   while($row = mysqli_fetch_array($result))
  {
  
  $pic =  $row['image']; 

  echo   $row['item'] ;
  echo  $row['location'];
  echo $row['description'];
  echo  $row['forum'];
  echo $row['datetime'];
  echo $row['username'];

 ?>


</br>


 <img src="upload/<?php echo  $pic ?>"/> 
 <?php echo  $row['image'];  } ?>
 "upload/<?php echo  $pic ?>"
 
 </body>
 </html>

enter image description here

As you can see it display everything except the img src.

enter image description here

This is my database (ignore BLOB that was a test). I can't seem to figure out where I'm going wrong. Thanks

Upvotes: 0

Views: 7232

Answers (6)

user1944305
user1944305

Reputation: 153

Well after 3 days I figured it out... my header location was incorrect! ../project/projectviewposted.php/ A stupid extra slash at the end!! Thanks for your help and suggestions!

Upvotes: 0

h4cky
h4cky

Reputation: 894

My recommendation is to store the full relative path in the database like this:

uploads/folder/file.jpg

My preferred MySQL field type is 'varchar(255)' the your echo in te PHP code will look like:

echo '<img src="'. $row['image'].'" />';

Upvotes: 1

risk
risk

Reputation: 918

You can try :

  while($row = mysqli_fetch_array($result))
  {

      $pic =  $row['image']; 

      echo $row['item'] ;
      echo $row['location'];
      echo $row['description'];
      echo $row['forum'];
      echo $row['datetime'];
      echo $row['username'];

      echo '<br /> 
            <img src="upload/'.$pic.'"/> 
            '.$row['image'];
  }

Upvotes: 0

Roger
Roger

Reputation: 1693

Try this

<img src="upload/<?php echo $pic; ?>"/> 
 <?php echo  $row['image'];  } ?>
 "upload/<?php echo  $pic ?>"

Else note down the link of the folder path. It might be that the folder path is not correct.

Hope this helps

Upvotes: 0

Raheel Hasan
Raheel Hasan

Reputation: 6023

1) view source (or use firebug) to see the image tag and see what is the src given there.

2) try the url: "http://{LOCAL}/projects/projectviewposted.php/upload/happyball(1).jpg" and see if you can open the image

Upvotes: 0

mbvlist
mbvlist

Reputation: 13

You are trying to print the image source outside the while-loop. The while-loop will only exit when $row is empty, so $row['image'] is also empty.

Upvotes: 0

Related Questions