Jump to content

Displaying image from database


Confuzzled

Recommended Posts

Hi,

 

I've got a site where that's got a database behind it. Currently it has loads of items in rows that all have different pictures. There is a field called "Image" that has the name of the image ie. example.jpg.

 

Then I have a folder on my webserver called image where the pictures are stored with the same file names and extensions.

 

Now, I want to display the correct image for the item that the page is showing. I have this code but it's just showing the usual box with a red X in the top left.

 

I should also point out that the 'Item' line is working correctly and displaying the name of the item.

 

<table border='1' align="left" width="100%">

<?php

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

$dir = '/image';

$image = $row['Image'];

echo('<b><h1>'.$row['Item'].'</h1></b><br />');

 

echo "<tr>";

echo "<td width=15%>"."<img width=100% src='$dir/$image /'></td>";

echo "<td width=85% align=center>Description</td>";

echo "</tr>";

}

?>

</table>

 

Any ideas why this isn't working? :wtf:

 

Many thanks for your time!

Link to comment
Share on other sites

Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)"

 

The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing.

 

If you both say they work, is it something to do with my browser or something?

 

Many thanks for the oh so quick replies  :D :D

 

Quick EDIT:  Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused  :wtf:

Link to comment
Share on other sites

my bad, this should do it:

 

echo '<td width="15%"><img width="100%" src="'.$dir.'/'.$image.'" /></td>';

 

my previous code was dividing $dir by $image, which is your error.

 

I'm sure it was, however, placing that code you just supplied in leaves the page with no image shown again :(

Link to comment
Share on other sites

Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)"

 

The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing.

 

If you both say they work, is it something to do with my browser or something?

 

Many thanks for the oh so quick replies  :D :D

 

Quick EDIT:  Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused  :wtf:

 

Sorry, I also took the '/' out of this line...

$dir = 'image';

 

I didn't use a database for testing, just a local image.

 

Link to comment
Share on other sites

Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)"

 

The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing.

 

If you both say they work, is it something to do with my browser or something?

 

Many thanks for the oh so quick replies  :D :D

 

Quick EDIT:  Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused  :wtf:

 

Sorry, I also took the '/' out of this line...

$dir = 'image';

 

I didn't use a database for testing, just a local image.

 

that code will show an image if there is an image to show .. check your source code and see what's being displayed in the <img> tag, and match it to what it should be.

 

also, turn error_reporting on:

error_reporting(1);

at the top of your script.

Link to comment
Share on other sites

Ok so, the example given by mrMarcus pulls me an error saying "Division by 0 in (filepath)"

 

The example given by Calver doesn't appear to affect the page as it still remains the same without the image showing.

 

If you both say they work, is it something to do with my browser or something?

 

Many thanks for the oh so quick replies  :D :D

 

Quick EDIT:  Both examples show the path as correct in the source as suggested by the first reply so i'm even more confused  :wtf:

 

Sorry, I also took the '/' out of this line...

$dir = 'image';

 

I didn't use a database for testing, just a local image.

 

that code will show an image if there is an image to show .. check your source code and see what's being displayed in the <img> tag, and match it to what it should be.

 

also, turn error_reporting on:

error_reporting(1);

at the top of your script.

 

This worked! Thankyou very much!

 

I will also turn on error reporting as you suggest. Cheers guys. What a great forum! I love it when people reply so quickly! :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.