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.

1
  • Do you accept "do not show the contents at all as PMA can't do it meaningfully" as a way to solve it? Commented Dec 22, 2012 at 14:05

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

$id = $_GET['id']; can u please tell me what is 'id', here? the name of the column of table?
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.
i have done the same, but now, nothing shows in the column named, pictures, its blank
What do you get, if you open your image php script in the Browser?
it shows an error, Undefined index: id..... on the line, $id = $_GET['id']; now, i have a column named, id, in my table gifts.
|
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"> 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.