Display images from Database in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Display images from Database in PHP

Post by morena84 »

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 everyone
I'm stuck again on Displaying images from a Database and I hope somebody will be able to help me.

Let me explain the scenario a bit:
- the images are uploaded through a PHP upload page which moves the images in a folder of my website
- at the same time the file name is inserted into a database:

table structure:
photo_id(mediumint, auto incremented, primary key)
photo_name (varchar 255, basicallly the $_FILES["file"]["name"])

inserting the image details into the database:

Code: Select all

$filename=$_FILES["file"]["name"];
        
    $sql="INSERT INTO nimtaz_photos (photo_name) VALUES ('$filename')";
    if (!mysql_query($sql,$dbc))
  {
  die('Error: ' . mysql_error());
  }

So far so good:
- The image is saved on my website,
- the image name is stored in my database as imagename.jpg or imagename.gif


In my photos.php page I would like to display all the images in a while loop but I don't know how to point to the image name on the database from my page:

Code: Select all

<?
    $query  = "SELECT * FROM nimtaz_photo ORDER BY photo_id DESC";
            
    $result = mysql_query($query);
            
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
    echo "<img src='http://www.morenafiore.com/public/upload/".$row['photo_name']"'" />";
?>
 

I tried them all:
- echo "<img src='http://www.morenafiore.com/public/upload/$row['photo_name']'
- echo "<img src='http://www.morenafiore.com/public/upload/'$row['photo_name']''
- echo "<img src='http://www.morenafiore.com/public/upload/{$row['photo_name']}'

So I hope somebody can help me understanding what I'm doing wrong :(
Thank you all
Morena


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

Re: Display images from Database in PHP

Post by pickle »

- Don't use short tags (<? ... ?>) - always use long tags (<?php ?> )
- You're missing a closing bracket in your second snippet - but I'm guessing that's just a copy-past error.

What does your page source code look like? Is it what you expect? What happens when you plug the address of the image directly into your browser?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: Display images from Database in PHP

Post by morena84 »

Hi Pickle

Sorry about the code not put into

Code: Select all

 
In ff the Code button does not work

Thanks for the suggestions
I have added the } and also amended the <? into <?php
but still having problems with

Code: Select all

echo "<img src='http://www.morenafiore.com/public/upload/$row['photo_name']' />";
Do you know if that is written in the right way?
Thanks again
Morena
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Display images from Database in PHP

Post by pickle »

The syntax highlighting should show you that it's not the right way. Look at the source code generated - that should help.

The [syntax=php]button works fine in Firefox - it just doesn't give you[/syntax][syntax=php]- you have to put the =php part in yourself.[/syntax]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: Display images from Database in PHP

Post by morena84 »

Hi There.
It might be my proxy, but when I clicked on Code nothing was added to my text.

Anyway, I forgot to mention that the page is not displayed at all and that I get this error

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\Inetpub\webs\morenafiorecom\nimtaz\photos.php on line 19
with this string:

Code: Select all

echo "<img src='http://www.morenafiore.com/public/upload/$row['photo_name']' />";
So I guess I am not inserting the $row['photo_name'] properly :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Display images from Database in PHP

Post by pickle »

Ya - parse errors are pretty much a show stopper.

As you know, $row['photo_name'] isn't a string, it's an array element - so it shouldn't be part of a string. I'd do a Google search to find how to include array elements in strings, or how to properly echo array elements.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: Display images from Database in PHP

Post by morena84 »

Thank you :)
morena84
Forum Newbie
Posts: 9
Joined: Thu Apr 03, 2008 9:52 am

Re: Display images from Database in PHP

Post by morena84 »

The correct sintax was <img class='img' src='http://www.morenafiore.com/public/upload/nimtaz/".$row['photo_name']."
:)
thanks everyone
Borojimmy
Forum Newbie
Posts: 12
Joined: Fri Mar 20, 2009 2:08 pm

Re: Display images from Database in PHP

Post by Borojimmy »

Hi,

I know this might ne a bit :offtopic: ....

But i have been trying to find a tutorial for this very subject somewhere on the net, is there any chance someone could point me in the right direction please?


Many Thanks

Jimmy
Post Reply