Page 1 of 1

I can't see an image stored in MySQL through PHP

Posted: Fri Jan 23, 2009 1:52 pm
by zenon
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.[/color]


Hello.

I' ve tried to make database.
I made an array, and the one element is a picture (blob type).

When i call it from php code, i can't see it :(
Can you help me please?
I would very glad!

This is my database:

Code: Select all

use brscn;
 
create table alldata
(id MEDIUMINT NOT NULL AUTO_INCREMENT,
captured DATE default NULL,
img longblob default NULL,
descr varchar(200) default NULL,
fnd varchar(200) default NULL,
biopsy_rsl varchar(200) default NULL,
primary key(id));
 
INSERT INTO alldata  VALUES (NULL, '2009-1-21', LOAD_FILE('C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images/mammography.jpg'), 'xxxxxxxxx','xxxxx','xxxxxxxxxxxxx');
Here is my php code:

Code: Select all

<?php 
                $con = mysql_connect("localhost:3307","root","xxxx");
                if (!$con)
                 {
                    die('Could not connect: ' . mysql_error());
                  }
                mysql_select_db("brscn", $con);
                
                
                $result = mysql_query("SELECT * FROM alldata");
                                $row = mysql_fetch_array($result, MYSQL_ASSOC);
                  {  
                     echo "Query results" . "<br />". "--------------------<br />" ;
                     
                     echo $row['id'] . " | ". $row['captured'] . " | " ;
                     
                     
                    ;
                     
                     $image = imagecreatefromstring($row['img']); //this is the pic
                     
                     if ($image !== false) {
                        header('Content-Type: image/jpeg');
                        imagegif($image); //output the image
                        imagedestroy($image); //free up memory
                    }
                    else {
                        echo 'An error occurred.';
                    }
                    
                    
                     echo " | " . $row['descr'] . " | " . $row['fnd'] . " | " . $row['biopsy_rsl'];
                     echo "<br />";
                 }
                mysql_close($con);?>
In my browser, i get this one:


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.[/color]

Re: I can't see an image stored in MySQL through PHP

Posted: Fri Jan 23, 2009 2:11 pm
by pickle
If you're outputing image data, you can ONLY output the image data - not any of that other stuff. When storing images in a database (not the best idea to begin with mind you), the general best practice is to have a single PHP file that you treat as an image file. So rather than putting:

Code: Select all

<img src = "/path/to/image.jpg" />
You call

Code: Select all

<img src = "/path/to/file.php?id=id_of_record_with_image" />

Re: I can't see an image stored in MySQL through PHP

Posted: Fri Jan 23, 2009 2:20 pm
by zenon
Thank you very much for your quick answer!

I want to show a picture that is stored in mysql database as a binary code, and not its path.
So i want to 'rebuild' it and show it.
If i try out the code you told me, isn't it like i'm presenting a picture from it's path?


Sorry for my bad English!
Thank you again!!

Re: I can't see an image stored in MySQL through PHP

Posted: Fri Jan 23, 2009 5:01 pm
by zenon
Now i understood what you meant before!

Is there a way to have pics and data from a database, in the same php file?

Re: I can't see an image stored in MySQL through PHP

Posted: Sat Jan 24, 2009 7:02 pm
by pickle
No. A php file can only pretend to be one kind of file at a time. By default it acts like HTML. You can make a PHP file also pretend to be an image, but not both in one file.

Re: I can't see an image stored in MySQL through PHP

Posted: Sun Jan 25, 2009 4:07 am
by zenon
pickle wrote:No. A php file can only pretend to be one kind of file at a time. By default it acts like HTML. You can make a PHP file also pretend to be an image, but not both in one file.
Hello.

Thank you very much for your answer!!

Have a nice day!