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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

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

Post 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]
Attachments
printView.JPG
printView.JPG (44.59 KiB) Viewed 856 times
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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" />
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

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

Post 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!!
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

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

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

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

Post 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!
Post Reply