I have successfully uploaded image to mysql database using a form: This is the code:

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");


if(isset($_REQUEST))
{


$imgtype=$_FILES;
$name=$_REQUEST;
$address=$_REQUEST;
$dateofbirth=$_REQUEST;


if($imgtype=="image/jpeg" || $imgtype=="image/jpg" || $imgtype=="image/pjpeg" || $imgtype=="image/gif" || $imgtype=="image/x-png" || $imgtype=="image/bmp")
{


$image=$_FILES;
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>

Now I need to display the row data including the image, can someone please offer a code that will solve the problem.

Thanks

Austin

basically you want to do something like this:

if ($_REQUEST[your_image_var] == 1) {
        header("Content-type: image/jpeg");
        print $bytes;
        exit ();
        }

Hello techniner ,

Please expand the code so that I can understand it better.

Thanks.

ive worked with images & mysql by uploading the file to (DIR)/upload then storing the directory only in mysql.

then using something like :-
<?php
$result=mysql_query("select * images where id = BLAH!");
$row=mysql_fetch_array($result);
?>

<html>
<body>
<img src="<?php echo $row[directory] ?>"/>
</body>
</html>

I have also done a bit with CMS which requires image storage on a LAMP system. Why not simply store the images in an image directory and refer to the image location in the database field. Okay it requires extra work using the file system, but to be honest I found it easier in the long run. PHP has some excellent fast functions for managing directories/files. You then retrieve and display the image as cmarkme suggests.

Please an I have the code for the display of the images and row data.

Thanks

Way I see it is this - you need four parts to your code. For preference I'd put things like database connections and paths to file storage in constants that you can include wherever you need them. The objective is to store your images in fixed directory, give them unique names if you need to, refer to the directory using a constant (so you can use it anywhere in your script) Store only the filenames in your database then when you need an image, you simply rebuild the path and filename by recombining the constant with the result of your SQL statement.

So you define the path to your image directory thus:

define("MAIN_IMAGE_STORE","../images/");

you then need to upload and store images in this directory thus:

$whereto=MAIN_IMAGE_STORE.$_FILES;
$result=copy($_FILES,$whereto);

(don't forget - never trust anything uploaded to your server!!!)

you also need to extract the filename from the uploaded file thus:

$image_name=$_FILES;

(you can do some checks here to see if the image-name already exists and inform your user accordingly)

You store this filename ONLY in the database. (You do not need to store the path - that is already set in your path constant - the beauty is if you need to change the path, you do so simply by creating the new directory and updating the constant.)

"UPDATE database_table SET my_image = '$image_name WHERE ... etc

To retrieve the image for display you create your SQL to fetch the required filename from the database, "SELECT my_image FROM database_table WHERE guid = xxxx.xxxx...." or whatever, and include this result into your IMG tag, see the code snippet given by cmarkme. HOWEVER this time you need to specify the location of the image by rebuilding the path from your constant plus the image-name you have retrieved from the database; which is of course (simplified as):

$image_name=$row;
or...
$full_path=MAIN_IMAGE_STORE.$image_name;
or...
<img src='".MAIN_IMAGE_STORE.$image_name."' ... etc>

Obviously your choice of syntax is personal in the code snippets above. You could build the entire path and filename first, or simply concatenate the whole thing into one line of code.

There is loads of stuff on this topic in the php manual. I hope this helps, it's a bit stilted I know and there are so many possibilities, but this should point you in one direction that will work a treat and is fairly efficient both from handling the images and database frugality.

So you define the path to your image directory thus:

define("MAIN_IMAGE_STORE","../images/");

The OP is saving the images in a table.

Hiya nav33n, yes fully understood, just a different way of doing things I guess. As I said, there are many ways of accomplishing the same job. The OP revealed a problem with retrieval of the image and display. Using this method elegantly negates the problem by allowing insertion of a filename, constructed on-the-fly so to speak into the html img tag. Horses for courses.

Yep.. And also, thats the best way, storing the path of the image instead of the image file. Displaying 1 image(if stored in a table), can be done. But I have no clue how to fetch all the images from the table. hmm!

In the link, he isn't displaying all the images. He's given a link to 'download' an image file. That again, is displaying 1 image at a time ! :@

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.