Hey guys,

Can you all help and teach me how to retrieve image file which is stored on database?..
I had an image stored on database in the column i called 'profpic'. By the way, I am needing this information for profile picture on a users page.
I've started testing some codes based on some sources but seems like I don't get the right procedure fitting for my program.
Here is the code segment

<?php 
					include 'dbconnect.php';
					echo 'under profile.php';
					$username=$_SESSION['username'];
					$query="SELECT accntnum FROM accntinfos WHERE username='$username'";
					$result=mysql_query($query, $DBconnect);
					if($accntnum=mysql_fetch_assoc($result))
						$accntnum=$accntnum['accntnum'];
					$query="SELECT profpic FROM userprofile WHERE accntnum='$accntnum'";
					$result=mysql_query($query, $DBconnect);
					//$numrows=mysql_num_rows($result);
					
					
					//PROBLEM STARTS HERE
					header('Content-type: image/jpeg');
                                        //i know there should be some proper codes here but I don't know how to start it... 
				?>

There are two tables here 'accntinfos' and 'userprofile'. These is just a segment but if you'ld need the two files for clarity just ask. Thanks

profpic is the image name or the image itself? What kind of column is profpic: blob or varchar?

Member Avatar for ampere

the thing i can say is don't use database to store images. Just store the images in a folder and just store the path in the database. So that it will be easy for you to retrieve and use image files in your pages. And by doing this you can also increase your db performance.

it's better you store the image links in your db. this will not make the database grow in size in short time and ...

The following code will solve your problem provided your image uploading script

  1. Retrieves the image name like xyz.jpg and store it in a specially created folder with a name like profile_picture.
  2. Inserts the name of the image in column profpic of your userprofile table.

In the img tag, you will need to specify the image width and height.

$query="SELECT profpic FROM userprofile WHERE accntnum='$accntnum'";
$result=mysql_query($query);
$nm=mysql_numrows($result);
$j=0;
while($j<$nm)
{   
    $photograph=mysql_result($result,$j,"profpic");

    $j++;
}

echo('<img width=  height=  border=none src='../profile_picture/'.$photograph.''>');    

You are advised not to store the image file directly in the database but in a special folder. What you should store in the table is the image file name.

Member Avatar for diafol

Depending on the images you're storing - I'd use links rather than storage, unless it's for something for something trivial like avatars.

Also if you're storing paths, try to make them as relative as possible, usually, just the filename if they're all stored in the same folder, otherwise, with just the subfolder name in the path. This may save headaches if you have to move server / change / rename the parent folder structure. Just keep the parent path in a config variable , e.g.

If your images are stored in these locations:

/uploads/images/2012/

/uploads/images/2011/

I'd store the path as:

2012/user431_7384792837.jpg

And the config var as: $img_parent = 'uploads/images/'; Just a thought...

thank you guyz!... I have changed my idea and I will store it on directories instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.