Page 1 of 1

Display images from Database in PHP

Posted: Tue Jan 27, 2009 10:02 am
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]

Re: Display images from Database in PHP

Posted: Tue Jan 27, 2009 10:18 am
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?

Re: Display images from Database in PHP

Posted: Tue Jan 27, 2009 10:37 am
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

Re: Display images from Database in PHP

Posted: Tue Jan 27, 2009 10:46 am
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]

Re: Display images from Database in PHP

Posted: Tue Jan 27, 2009 10:59 am
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 :(

Re: Display images from Database in PHP

Posted: Tue Jan 27, 2009 11:05 am
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.

Re: Display images from Database in PHP

Posted: Wed Jan 28, 2009 3:17 pm
by morena84
Thank you :)

Re: Display images from Database in PHP

Posted: Sat Jan 31, 2009 6:17 pm
by morena84
The correct sintax was <img class='img' src='http://www.morenafiore.com/public/upload/nimtaz/".$row['photo_name']."
:)
thanks everyone

Re: Display images from Database in PHP

Posted: Sat Mar 21, 2009 4:24 am
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