SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
-
Jun 11, 2002, 16:08 #1
- Join Date
- May 2002
- Location
- Ireland
- Posts
- 37
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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);
?>
-
Jun 11, 2002, 18:16 #2
- 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.
-
Jun 12, 2002, 08:48 #3
- 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\" />
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.
-
Jun 12, 2002, 09:09 #4
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>
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!
-
Jun 12, 2002, 09:53 #5
- 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 know you all find this very easy, but I don't right now, so please be patient!
-
Jun 12, 2002, 10:01 #6
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. "\">";
Last edited by notepad_coder; Jun 12, 2002 at 10:04.
- the lid is off the maple syrup again!
-
Jun 12, 2002, 10:36 #7
- 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.
-
Jun 12, 2002, 10:37 #8
- 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. "\">";
But everytime I just get Parse Error: expecting '," or ';" in c:\phpdev\www\show_prod.php on line 17
-
Jun 12, 2002, 10:47 #9
- 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.PHP Code:<?php
echo "<img src=\"pics/$Image_Loc\" />";
?>
-
Jun 12, 2002, 10:52 #10
- 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\" />
Sorry about that
Bookmarks