SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Ireland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Please Help: Display Image from database

    Hi,
    Can someone help me, I'm a real newbie to this! I'm trying to save a link to a picture in mysql and then load the picture on a webpage,it's a product catalog. In the Image_Loc column I just have pic1.jpg as the link name and then I was putting the image in the folder c:\phpdev\pics and just using <img src=/phpdev/pics/$Image_Loc /> to call the pictures.
    Code below, I have changed it a few different ways, but everything I tried just gives a box with an X as if the picture doesn't exist. I tried giving it full address i.e.: http://localhost/Pics/$image_loc etc. still no joy
    PHP Code:
    <?php 
    $connection 
    mysql_connect("localhost""root""") or die ("Couldn't connect to server.");
    $db mysql_select_db("XYZPrint"$connection) or die ("Couldn't select database");
    $sql "SELECT Product_ID, Title, Image_Loc
        FROM Products"
    ;
    $sql_result mysql_query($sql$connection) or die ("Couldn't execute query.");

    // start results formatting
    echo "<table border=1>";
    echo 
    "<TR><TH>Product ID</TH><TH>Title</TH><TH>Picture</TH></TR>";
    while (
    $row mysql_fetch_array($sql_result)) {
    $product_id $row["Product_ID"];
    $title $row["Title"];
    $Image_Loc $row["Image_Loc"];
    echo 
    "<TR><TD>$product_id</TD><TD>$title</TD><TD><img src=http://localhost/phpdev/Pics/$Image_Loc</img></TD></TR>";
    }
    echo 
    "</TABLE>";
    mysql_free_result($sql_result);
    mysql_close($connection);
    ?>
    Thanks In Advance!!!

  2. #2
    SitePoint Evangelist CyberFuture's Avatar
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is the folder name "pics" or "Pics"? Though on a PC, I don't think it matters. The problem I see is your image tag is all wrong.

    Try this:
    <img src="pics/<?=$Image_Loc ?>" />

    or in an echo statement:
    <img src=\"pics/$Image_Loc\" />

    This should work as long as the "pics" folder is in the same folder as your script.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Ireland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or in an echo statement:
    <img src=\"pics/$Image_Loc\" />
    I tried this but it didn't work, I'm confused because the pics folder isn't in the www folder, so how do I put in the directory. I have a phpdev folder with www folder inside it and it contains my php script, the Pics folder is also in the phpdev folder. I tried with <img src=\"Pics/$Image_Loc\" />.
    It's probably something silly that I'm missing, I'm calling the <IMG> tag from inside a table in an echo statement, does this matter??
    Thanks.

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you using this?
    PHP Code:
    <img src=http://localhost/phpdev/Pics/$Image_Loc</img>
    // shouldn't it be this
    <img src=http://localhost/phpdev/Pics/$Image_Loc> 
    You dont use </img> to close image tags. it does it all in one.

    And make sure you have an extension on the image in that database and not just the name like something.png or something like that.
    - the lid is off the maple syrup again!

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Ireland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you using this?
    PHP:
    --------------------------------------------------------------------------------
    <img src=http://localhost/phpdev/Pics/$Image_Loc</img>
    // shouldn't it be this
    <img src=http://localhost/phpdev/Pics/$Image_Loc>
    I am now using your second line, and I tried changing the database to have c:\phpdev\Pics\pic1.jpg but it won't put in any of the "\" so instead I'm left with c: phpdevPicspic1.jpg in MySql table. (without the space)

    I know you all find this very easy, but I don't right now, so please be patient!

  6. #6
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try changing c:\phpdev\Pics\pic1.jpg to pic1.jpg in the database.

    In the database all you need is the image name. You don't need the path or anything since you have it in the image tag.

    I would do it like this if the file you are listing the images on is in phpdev as well
    PHP Code:
    <img src=http://localhost/phpdev/Pics/$Image_Loc>
    // change to 
    <img src=\"Pics/" .$Image_Loc"\">"
    you don't have to use the " .$. " I just do it for easier reading.
    Last edited by notepad_coder; Jun 12, 2002 at 09:04.
    - the lid is off the maple syrup again!

  7. #7
    SitePoint Evangelist CyberFuture's Avatar
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    elicaf,

    The "pics" folder MUST be inside of the "www" folder, since localhost's root folder is "www". Moving the folder should solve your problem.

    As for echoing the image tag, remember the image src MUST be in quotation marks(single or double). This is required for both HTML and XHTML.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Ireland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope still not working! What's the extra " in your code for?
    PHP Code:
    <img src="Pics/" .$Image_Loc"\">"
    You seem to have 5 " in it so one isn't closed??? I tried putting "<img src="Pics/" .$Image_Loc. "\">"; and I tried it without like: <img src="Pics/" .$Image_Loc. "\">;
    But everytime I just get Parse Error: expecting '," or ';" in c:\phpdev\www\show_prod.php on line 17

  9. #9
    SitePoint Evangelist CyberFuture's Avatar
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the value of $Image_Loc? It should just be the file name.

    The code you just posted is wrong. It should be one of the following:
    Try this for normal html:
    <img src="pics/<?=$Image_Loc ?>" />

    or in an echo statement:
    <img src=\"pics/$Image_Loc\" />

    This should work as long as the "pics" folder is in the same folder as your script.
    The full line of the last example should look something like this:
    PHP Code:
    <?php
    echo "<img src=\"pics/$Image_Loc\" />";
    ?>

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Ireland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Panic Over!!!

    Hi & Thanks,
    I worked it out! I'm using this:
    PHP Code:
    <img src=\"pics/$Image_Loc\" /> 
    as CyberFuture told me earlier, I just messed it up when the Pics folder were in the wrong place, had to put it in the XYZCompany folder inside www folder,
    Sorry about that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •