i though repost this issue. i am trying to display image from database. but broken image shows up. and dont how why.
i tried debuging like echo, mysql_error, erron, but there are no errors.

get.php
<?php 
include("connect.php");

$user_id = $_SESSION['user_id']; 

$image = mysql_query("SELECT * FROM image WHERE user_id = $user_id"); 
$image = mysql_fetch_assoc($image); //get access to image table
$image = $image['image'];
header("Content-type: image/jpeg");
echo base64_decode($image); 
?>  


upload.php file
$lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p />        //display image here
                          <img src=\"get.php?image_id=$lastid\"></img>

Recommended Answers

All 16 Replies

When inserting the image in database you use base64_encode()?
P.S. It would be better to post the code where you insert it in database.

upload.php file

$image = base64_encode(file_get_contents($_FILES['fileupload']['tmp_name']));
            //store image name in $image_full_name(.jpeg)
            $image_full_name = $_FILES['fileupload']['name'];
            //store image size in $image_size
            $image_size = getimagesize($_FILES['fileupload']['tmp_name']);
            //test if its a image or file
            if($image_size == FALSE)
            {
                echo "Thats not a image";
            }
            else
            {
                //get user id and sote in user_id_db
                $queryget = mysql_query("SELECT user_id FROM user WHERE username = '$user'") or die("query didnt work");
                $row = mysql_fetch_assoc($queryget);    
                $user_id_db = $row['user_id'];

                //insert information into database
                if(!$insert = mysql_query("INSERT INTO image VALUES(NULL, '$user_id_db', '$image', '$image_full_name', '$image_short_name', '$image_des',0,0)"))
                {
                    echo "problem uploading image";
                }
                else
                {
                    $lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p />
                          <img src=\"get.php?image_id=$lastid\"></img>
                          ";

                }

get.php

posted above

You wrote in your query in get.php:

user_id = $user_id

But you request the image with image_id and you dont use it.

get.php?image_id=$lastid

You need to modify your query in:

"SELECT * FROM image WHERE 'image_table_first_field' = $_GET['image_id']"

What is datatype of image field in mysql table?

commented: blob +0

P.S. Don't forget to change image_table_first_field to the real one...

Also it woud be better to change

mysql_fetch_assoc

to mysql_fetch_row

Your image's field datatype must be BLOB or LONGBLOB.

commented: thanks +0

"SELECT * FROM image WHERE 'image_table_first_field' = $_GET['image_id']"

not sure if i understant image_table_first_field = $_GET[...]

here is my image database
image_id
user_id
image(store image here)
image_full_name
image_short_name
..etc

by doing

$user_id = $_SESSION['user_id']; 

i am getting the user id who ever is loged in.

than iam find user_id(who is loged in) in image data.

$image = mysql_query("SELECT * FROM image WHERE user_id = $user_id");

after that iam geting image of who is loged in by

$image = mysql_fetch_assoc($image); //get access to image table

What is datatype of image field in mysql table?

blob

When you insert image in database use chunk_split function as shown below.

$image = chunk_split(base64_encode(file_get_contents($_FILES['fileupload']['tmp_name'])));

After that check, if still it is not working comment below line
// header("Content-type: image/jpeg");

and open get.php in other tab of browser, check if you having any error?

commented: yeahh +0

i got 2 errors
Notice: Undefined variable: _SESSION in C:\xampp\htdocs\login_test\get.php on line 4

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login_test\get.php on line 7

1st one is it bc i have to do session_start()?

yeahhhhh got it working.

thanks vibhaj and spart

just a quick question is it way to make this code smaller? i really dont know what this get.php code is doing.
get.php file
<?php 
session_start();
include("connect.php");

$user_id = $_SESSION['user_id']; 

$image = mysql_query("SELECT * FROM image WHERE user_id = $user_id"); 
$image = mysql_fetch_assoc($image); //get access to image table
$image = $image['image'];
//header("Content-type: image/jpeg");
echo base64_decode($image); 
?>  


upload.php   to display image
$lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p />
                          <img src=\"get.php?image_id=$lastid\"></img>

oh also

$lastid = mysql_insert_id(); 
                    echo "Image upoaded.<p />your image:<p />
                          <img src=\"get.php?image_id=$lastid\"></img>

this always print the 1st image in database. how do i print the last image?
i think its bc i need to put some thing in mysql_insert_id(); function

$image = mysql_query("SELECT * FROM image WHERE user_id = $user_id ORDER BY image_id DESC");
This will show the last image of the user $user_id.
The last image added you can show by:
$image = mysql_query("SELECT * FROM image ORDER BY image_id DESC");
And because you send the image_id as GET parameter to the page get.php you could use
$image = mysql_query("SELECT * FROM image WHERE image_id = " . $_GET['image_id']);

                $lastid = mysql_insert_id(); 
                echo "Image upoaded.<p ></p>your image:<p ></p>
                      <img src=\"get.php?image_id=$lastid\"></img>

this right here print the last image in database of whose ever is loged in. (print 1 image) works ok

what if i want to print all the images in database of whoese ever is loged in. (print all the images).

also if i use echo it print image a top of the screen. what is i want to put in a table. so ex.

1 = table lines
2 = images

 -------
 1212121
 1212121
 -------

so above has 2 rows and 3 cols. i want to do it so it keeps on printing so last image is at top left spot.
i now how to make hhtml table but i dont know how to combine php and html code so i get this.

To get all user images you need something like this:

    <?php
        $q = mysql_query("SELECT image_id FROM image ORDER BY image_id");
        while($im = mysql_fetch_assoc($q)) {
            echo '<img src="get.php?image_id=' . $im['image_id'] . ">';
        }

In get.php you should use this query:

$image = mysql_query("SELECT * FROM image WHERE image_id = " . $_GET['image_id']);

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.