By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

view photos in a folder via the mysql

P: 9
I have read and read about mysql, which I am sure is just like the quoted, "linux is very friendy, but it just chooses who it wants to be friends with..."

My first attempt to read the database in my forum actually works! Well, almost. It does query the correct places, lists the information, and I have even figured out how to see a full path to where the photo is stored.

But is never actually SHOWS the image.

My eventual goal is to simply input a member id# and it will list all of the photo attachments by that member.

Right now I would settle for it to work by displaying the photos found, (which does actually shows the path now, but not as a link or photo.) src and all that simply does not work, header ("Content-type: image/jpeg"); print $imagebytes;, fails as well, at least in any way I have tried.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $username="MyUserName";
  3. $password="MyPassWord";
  4. $database="MyDataBase";
  5. $x=date("j");
  6.  
  7. mysql_connect(localhost,$username,$password);
  8. @mysql_select_db($database) or die( "Unable to select database");
  9. $query="SELECT * FROM ibf_attachments";
  10. $result=mysql_query($query);
  11.  
  12. $num=mysql_numrows($result);
  13.  
  14. mysql_close();
  15.  
  16. echo "<b><center>Database Output</center></b><br><br>";
  17.  
  18. $i=0;
  19. while ($i < $num) {
  20.  
  21. $member=mysql_result($result,$i,"attach_member_id");
  22. $isimage=mysql_result($result,$i,"attach_is_image");
  23. $imagedate=mysql_result($result,$i,"attach_date");
  24. $imagesize=mysql_result($result,$i,"attach_filesize");
  25. $imagewidth=mysql_result($result,$i,"attach_img_width");
  26. $imageheight=mysql_result($result,$i,"attach_img_height");
  27. $imagelocation=mysql_result($result,$i,"attach_location");
  28.  
  29. // this sets the image location to a full path
  30. $full_location = "http://www.MySite.com/forum/uploads/$imagelocation";
  31.  
  32.  
  33.  
  34. echo "<b>$member $isimage</b><br>
  35. Image Date: $imagedate<br>
  36. Image Size: $imagesize<br>
  37. Image Width: $imagewidth<br>
  38. Image Height: $imageheight<br>
  39. location: $imagelocation  <br>
  40. <br><br>
  41.  
  42. // this actually shows the link, but not clickable, nor will it display image here.
  43. // I can copy and paste it in browser and it is correct path.
  44. full location: $full_location<br>
  45.  
  46. <hr><br>";
  47.  
  48. ?>
  49.  
  50. <?PHP
  51.  
  52. $i++;
  53. }
  54.  
  55. ?>


any help would be apprecitated!
Nov 29 '09 #1

✓ answered by Frinavale

Your problem has very little to do SQL.
In fact it sounds like you're connecting to the database and retrieving the data correctly!
Your MySQL Database simply holds the URLs to the images that you want to display.

MySQL is a database of information...it's not supposed to "Display" anything at all. It just holds information. It is the browser's job to display things...and the only way the browser is going to know How you want to display something is if you use HTML to describe how you want things to be displayed.

So, when you retrieve the data from the SQL database you need to create/generate HTML to tell the web browser how to display the data. This means that if you want the web browser display an image, you have to use the HTML <img> tag to display the image.

Did you try:
Expand|Select|Wrap|Line Numbers
  1. echo "<b>$member $isimage</b><br>
  2. Image Date: $imagedate<br>
  3. Image Size: $imagesize<br>
  4. Image Width: $imagewidth<br>
  5. Image Height: $imageheight<br>
  6. location: $imagelocation  <br>
  7. Image: <img src='$full_location' alt='the image' />  <br>
  8. <br><br>";
???

-Frinny

Share this Question
Share on Google+
22 Replies


Dormilich
Expert Mod 5K+
P: 8,639
if you want to display an image in HTML you have to use the <img> tag pointing to the picture, which may be either the file of the image or a script file, that fetches the image and returns the binary data.
Expand|Select|Wrap|Line Numbers
  1. // in HTML (image from DB)
  2. <img src="image.php?id= xyz" width="…" height="…" alt="…">
Expand|Select|Wrap|Line Numbers
  1. // image.php
  2. header("Content-Type: image/jpeg"); // or whatever image type you have
  3. // connect to DB
  4. // query DB for image (using $_GET)
  5. // return data:
  6. echo $image;
  7. // close DB
see also the Insights Article Uploading files into a MySQL database using PHP (chapter 4)

PS. the only functions/packages that can fetch the whole result set into an array are PDO and MySQLi, both available in PHP 5 …
Nov 29 '09 #2

P: 9
thanks. I appreciate your attempt to assist, It doesn't help me, as I do not understand it, but I appreciate it. (and I do have php5 on the server)

I need to know how to make $imagelocation in the above PHP file show as a photo, not a non-clickable link.
Nov 30 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
Hi skysober,

I am by no means a PHP expert but I can explain why your image doesn't show up.

First of all, in order to display an image in your web page you have to use the HTML <img> tag. See w3c for more information about the HTML img tag.

When a webpage renders an image it needs to retrieve that image from the web server. That means that the image has to exist on the web server in order for the browser to retrieve it.

In your case there is no image on the web server for the web page to retrieve....your image is stored in a database.

So, how do you get around this?
Well, instead of having the web page retrieve the image from a file on the web server, have it retrieve the image by calling a php script that retrieves the image from the database.

This php script does not return HTML like a normal php script would.
Instead this php script will retrieve the bytes that are the image from the database. Once the php script has retrieved the image it will send the bytes (that are the image) to the browser.

Since the script does not return HTML you have to change the Response Content-Type Header. That way when the image is sent to the browser, the browser will know that it is an image (and not HTML) so that it can render it properly.

With this in mind, take a look at the suggestion that Dormilich posted.
He suggested that you create a php file that reads the image from the database into memory...change the Content-Type to "image"...and send the image to the browser.

In order for your page to display the image you need to use an HTML <img> tag (Dormilich has posted code showing you how to use this).

The <img> tag has to call the php script that returns the image in order to display the image in the web page. You will have to provide the ID of the image as a parameter to the php script so that it can retrieve the right image......

In order words, take a look at what Dormilich has recommended ;)

-Frinny
Nov 30 '09 #4

P: 9
The image is on the server. It is in the uploads folder. If I copy paste the result of $imagelocation into my browser, it shows the image just fine.

I have no clue as to what a somthing.php?something means. I only know html. basic PHP, an almost no SQL. This was my first attempt at reading a SQL, something that is probably quite easy for some, but is quite confusing to me.

I do know the images themselves are not stored in mysql, but actually stored in the upload folder and the mysql records that location. That confuses me a lot when someone says they are stored in the mysql.

Thanks!
Nov 30 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
I'm not really sure that I understand your problem completely but one thing is for sure: the image is not stored in a database.

If your image is on the server, then it has to be in a directory that is accessible to web browsers.

Make sure that your image is moved to a directory that is in your website.
Once it is there your img tag will be able to download it.

For example, if you move your image to a folder called "images" that is within your website on your sever your img tag would look like:

Expand|Select|Wrap|Line Numbers
  1. <img src='www.mydomain.com/images/theImage.jpg' alt='the image' />
  2.  
In your case the URL to the image is stored in the $full_location variable...so you would have something like:

Expand|Select|Wrap|Line Numbers
  1. <img src='<?php echo $full_location; ?>' alt='the image' />
I have no idea how your database is even involved in this problem...???

-Frinny
Nov 30 '09 #6

P: 9
Thanks, but this is turning away from the sql problem. HTML I know. I can make a link to a photo in a folder.

The mySQL simply stores the photos locations. When a member uploads a photo into uploads, the sql records this. I wish to read the sql, have it finds the name and location of the photos, and and then show the photos by that member.

My original code that I made at the beginning of this thread does all this, except actually showing the photos. Instead of the photos, it gives a text of "http://www.MySite.com/forum/uploads/NameOfThe1stPhoto.jpg." , "http://www.MySite.com/forum/uploads/NameOfThe2ndtPhoto.jpg." , etc.
Nov 30 '09 #7

P: 9
this is my present result when I run the peek.php

319 1
Image Date: 1235181668
Image Size: 88175
Image Width: 739
Image Height: 1000
location: monthly_02_2009/post-319-1235181668.jpg


full location: http://www.MySite/forum/uploads/mont...1235181668.jpg
Nov 30 '09 #8

Frinavale
Expert Mod 5K+
P: 9,731
Your images are stored on the web server, in a folder in the website.
Your database stores URL information that can be used to display the images uploaded by the user.

You have SQL that retrieves the URLs for the photos.....

You know that you need an <img> tag to display an image....

Why can't you display the images??

-Frinny
Nov 30 '09 #9

P: 9
Now that is the $64,000 question that I asked here in the first place ;)

As I just posted the result when I run peek.php, it does NOT show the photo.
Nov 30 '09 #10

Frinavale
Expert Mod 5K+
P: 9,731
Oh wait a second, you don't want to display the image (maybe??)...you just want to create a hyperlink to the image???

A hyperlink (a "link") is created using the HTML <a> tag (an anchor).

In this case you would use the <a> tag instead of the <img> tag and use the href property instead of the src property....

Do you have a public link to this website?

-Frinny
Nov 30 '09 #11

Frinavale
Expert Mod 5K+
P: 9,731
I have a feeling that you aren't creating the URL to the image properly.
Have you researched the $_Server variable?

$_SERVER[’PHP_SELF’], $_SERVER['REQUEST_URI'], and $_SERVER[’SCRIPT_NAME’] all return information about the php script that was requested. Maybe you could use one of these to find the URL to the images?

Like I said I'm not a PHP expert, so I'm not entirely sure what the best way to get the URI of your website.....


One other thing, are you Sure that the uploads folder is accessible?

-Frinny
Nov 30 '09 #12

P: 9
the resulting url is perfect. I can copy paste it into a new browser page and it works 100% showing the photo.

thanks. I give up on this. reading SQL is not going to show the photo no matter what I try. I have spent about 25 hours fighting this. I'm sure an sql xpert does this on a daily basis and is a joke for him, but I can't figure it out.

Thanks for trying!
Nov 30 '09 #13

Frinavale
Expert Mod 5K+
P: 9,731
Your problem has very little to do SQL.
In fact it sounds like you're connecting to the database and retrieving the data correctly!
Your MySQL Database simply holds the URLs to the images that you want to display.

MySQL is a database of information...it's not supposed to "Display" anything at all. It just holds information. It is the browser's job to display things...and the only way the browser is going to know How you want to display something is if you use HTML to describe how you want things to be displayed.

So, when you retrieve the data from the SQL database you need to create/generate HTML to tell the web browser how to display the data. This means that if you want the web browser display an image, you have to use the HTML <img> tag to display the image.

Did you try:
Expand|Select|Wrap|Line Numbers
  1. echo "<b>$member $isimage</b><br>
  2. Image Date: $imagedate<br>
  3. Image Size: $imagesize<br>
  4. Image Width: $imagewidth<br>
  5. Image Height: $imageheight<br>
  6. location: $imagelocation  <br>
  7. Image: <img src='$full_location' alt='the image' />  <br>
  8. <br><br>";
???

-Frinny
Nov 30 '09 #14

Markus
Expert 5K+
P: 6,050
I'm having a hard time following this thread (be it through no fault of your own, I'm just slightly sleepy with some wine in my blood).

Let us reiterate the exact problem; I don't want to know about anything that isn't specifically related to this problem - that only clouds my vision.

So, try this: *what* are you trying to do? What have you tried? What *isn't* working? What *is* working? What *errors* do you get?

No need to give up :)

Mark.

Edit: unless Frin's latest post clears the situation up.
Nov 30 '09 #15

P: 9
wtf! You code works! I dunno what u did dif than I did with the src, but yours shows the photo! Now I can actually start to work with it where I can choose it to only show a member specific photos.

THANK YOU SO MUCH!!!!!
Nov 30 '09 #16

Frinavale
Expert Mod 5K+
P: 9,731
Hah :)

I'm glad you solved your problem.

Happy coding!

-Frinny
Nov 30 '09 #17

P: 9
Thanks again! I can honestly say I think SQL is a neccesary EVIL ;)


hehe...

THANKS for the help!!!
Dec 1 '09 #18

Dormilich
Expert Mod 5K+
P: 8,639
I can honestly say I think SQL is a neccesary EVIL
I wouldn’t go as far as that… after all, it’s logical
Dec 1 '09 #19

Markus
Expert 5K+
P: 6,050
@skysober
You'd be in a hell of a worse situation if you didn't have SQL (or any other query language). Flat file database? Ick!
Dec 1 '09 #20

Dheeraj Joshi
Expert 100+
P: 1,123
Yeah Mark, if there was no SQL, i will be killing myself :)

Regards
Dheeraj Joshi
Dec 1 '09 #21

Frinavale
Expert Mod 5K+
P: 9,731
I can honestly say I think SQL is a neccesary EVIL
SQL is not evil once you understand it and how databases work.

It's funny but I do the same thing: I blame the thing I know the least about for the problems I'm experiencing. Sometimes it takes a while to see that it's something that I know that's causing the problem.

-Frinny
Dec 1 '09 #22

Markus
Expert 5K+
P: 6,050
@Frinavale
It's always much easier and more satisfying to blame anyone and anything other than yourself ^_^
Dec 1 '09 #23

Post your reply

Sign in to post your reply or Sign up for a free account.