0

i want to display the images, stored in a table, in PHP DataBase, table name is "gifts" and the column containing the images is named as, "pics"

(i know this is not a good approach but i need to do it)

now when i run the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org  /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

 {

  die('Could not connect: ' . mysql_error());

 }


mysql_select_db("sendemotions", $con);


$result = mysql_query("SELECT * FROM gifts");


echo "<table border='1'>

 <tr>

  <th>Item Picture/th>

 <th>Name Of Item</th>

 <th>Item Type</th>

 <th>Item Price</th>


</tr>";


while($row = mysql_fetch_array($result))

  {

 echo "<tr>";


 echo "<td>" . $row['pics'] . "</td>";

 echo "<td>" . $row['g_name'] . "</td>";

   echo "<td>" . $row['g_price'] . "</td>";

  echo "<td>" . $row['g_type'] . "</td>";


  echo "</tr>";

 }

 echo "</table>";


mysql_close($con);

?> 

</body>

</html>

i get a table displayed bu its has weird values in the column named, pictures: this is what the output is

please tell me what is the error and how to slove this :( i badly need to do it.

Saibamen
  • 610
  • 3
  • 17
  • 43
Momo Pomo
  • 265
  • 2
  • 5
  • 19

3 Answers3

2

I think you need a second php script to fetch the image. This script then needs to send the image header to display an image. Examle would be something like:

<?php

$id = $_GET['id']; #WARNING INSECURE. FOR DEMO ONLY!!!

connectToDatabase(); #However you do it

$sql = "SELECT pics FROM gifts WHERE ID = $id";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['pics'];
$db->close();

?>

WARNING: Abovo code is highly insecure!!! You need to check if $_GET['id'] is integer!

In your original php script you would then include this in the imagerow.

echo '<td><img src="http://yourdomain/imagescript.php?id="' . $row['id'] . '"></td>';

This code is not testet... just a quick overview for you.

pixelsucht
  • 228
  • 2
  • 8
  • $id = $_GET['id']; can u please tell me what is 'id', here? the name of the column of table? – Momo Pomo Dec 22 '12 at 16:15
  • It is the column "id" in the table. (I hope you have an id field in the gifts table?). ?id="'.$row['id'].'" sends this id via url. $_GET['id'] requests the id from the url. – pixelsucht Dec 22 '12 at 17:02
  • i have done the same, but now, nothing shows in the column named, pictures, its blank – Momo Pomo Dec 22 '12 at 20:15
  • What do you get, if you open your image php script in the Browser? – pixelsucht Dec 23 '12 at 07:00
  • it shows an error, Undefined index: id..... on the line, $id = $_GET['id']; now, i have a column named, id, in my table gifts. – Momo Pomo Dec 23 '12 at 11:47
  • O.K. I'm sorry but you definitely need to learn more about php, html and mysql before you can solve this problem. It would take Weeks to tell you everything here in the comments. The solution i told you works. You must now find out what it is all about :) Sorry. – pixelsucht Dec 23 '12 at 15:02
2

First of all you have to learn HTML basics.
In HTML, images being shown via <img> tag, given URL, pointing to the resource.

Next, instead of storing images in the database, which is quite unnatural for the files, place them in the directory. If you rename them after id you won't need even to store the path in the database - just construct it in the fly:

<img src="/img/gifts/<?=$row['id']?>.jpg">
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

I suppose you are storing the image in the database as Blob. Then you have to set header content-type in the php output.
Check this link show image from blob mysql

Another method is to use base64_encode
base64_encode
displaying an image stored in a mysql blob

Community
  • 1
  • 1
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47