Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying image stored MYSQL field in HTML table

Status
Not open for further replies.

dhammes

Technical User
Jun 15, 2007
3
US
i am a novice at using PHP and trying to do a very simple listing of items with a photo. The following code works fine for displaying the information in 2 columns however the images are not loading. The plan is to have the page loop through all the records in the recordset and display the data in 2 columns. Column one with the text - column two with image. I am using dreamweaver for creating the pages. I am sure it is something simple.

Help!!

thanks - domonic

<?php

mysql_select_db($database_nmma, $nmma);
$query_Provider = "SELECT * FROM providers";
$Provider = mysql_query($query_Provider, $nmma) or die(mysql_error());
$totalRows_Provider = mysql_num_rows($Provider);

while ($row_Provider = mysql_fetch_assoc($Provider)){
?>

<table width="500" height="71" border="0" class="subHeader">
<tr>
<td width="300"><span class="style6"><b><?php echo $row_Provider['ProviderName']; ?> <br />
<span class-"smalltext"><?php echo $row_Provider['Speciality']; ?></span></td>
<td width="200"><?php echo $row_Provider['Photo']; ?></td>
</tr>
<?php
}
?>
 
A couple things here first, are you actually storing the photo itself in the database, or just a link or path to the photo?

In either case you obviously need an image tag <img src="path\to\photo\file"> to display images.

And if your storing the actual picture in the database, then you need quite a bit more code to get it to display the way you want to.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the image is stored in the database - that is my preferred method to store them. it is in a longblot field.

is the code extensive? I would appreciate any help you could give.

- thanks
 
I and most of us here, would recommend against storing the images themselves in the DB, but since its already done, I'll guess I'll explain a bit more of what you need to do to get the images to display:

You need to create a separate script file that fetches the image from the DB and output's it out providing the correct headers. This script needs to be called from the image tag.


so for example you would have in your display page the following:
Code:
        <td width="300"><span class="style6"><b><?php echo $row_Provider['ProviderName']; ?> <br />
            <span class-"smalltext"><?php echo $row_Provider['Speciality']; ?></span></td>
        <td width="200">[red]<img src="getimage.php?providerId=ID"[/red]></td>
      </tr>

[green]//ID would be the unique identifier for the provider in your table. So you know which photo to retrieve[/green]

Then in your getimage.php script:

Code:
[green]//Again run a query, to get the photo to be displayed using the ID passed to it by providerId[/green]

$query_photo="SELECT Photo from providers where id_provider=$_GET['providerId']";
[green]//run query, and put contents of photo field into a variable
Send out appropriate headers and echo out Photo:
[/green]

header('Content-Description: File Transfer');
header('Content-Type: image/jpeg');
header('Content-Length: ' . strlen($Photo_var));
echo $Photo_var;




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Vacunita - thanks for the help - you were extremely helpful. I followed your first advice and placed the images in a folder and referenced the images in the Photos field - i changed the field to varchar and put the link to the file in quotes in the database. i.e. "/photos/shs.jpg"

After a bit of playing with the code, i added the quotes in the field and it works perfectly. Not the best, but this project is a mock up for a much larger project.




 
Glad I could help. Basically it is simpler to just link to an image, than to actually do all the extra coding to show it from a DB. Also Its easier to maintain images in folders, than it is inside a database.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
not to mention that when a database corrupts you lose all your binaries... whereas file systems are somewhat less likely to do so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top