2

I am having trouble with displaying images with php. I have tried this question, but it's still not displaying properly.

I have a PHP file that is handling the HTML format, the code for the image part is as follows:

while($row = mysql_fetch_array($result)) {
$partNo = $row['partNo'];
echo "<tr>";
echo '<td> <img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" /> </td>';

My getThumb.php is as follows:

  <?php

$id = $_GET['id'];

$con = mysql_connect("localhost", "root", "root") or die("Unable to connect to server");
mysql_select_db("bmworld.mu");
$sql = "SELECT thumbnail FROM part WHERE partNo=$id" or die ("Could not fetch thumbnail");
$result = mysql_query("$sql");
$row = mysql_fetch_object($result);
mysql_close($con);

header("Content-type: image/jpeg");
echo $row['thumbnail'];
?>

The results is as follows:

http://postimg.org/image/xqcq16ucf/

My image is of type 'blob' saved in MySQL, and not a path to the image folder. I want to retrieve that blob from MySQL, and display it directly from the retrieved blob instead of a path to the image, just like the other columns of data from my database table. How can I do this?

Community
  • 1
  • 1
  • I think you are right. I just tried moving getThump.php out of my working directory and the result is still the same as in the screenshot. How can I fix this? Where is my path of images? I thought the image was being retrieved directly from mysql? – Mephasm Vortex Mar 29 '15 at 18:52
  • My image is of type 'blob' saved in mysql, so I do not have the full path to the image folder. I want to retrieve that blob from mysql, and display it directly from the retrieved blob instead of a path to the image, just like the other columns of data from my database table. How can I do this? – Mephasm Vortex Mar 29 '15 at 19:16
  • Thank you for your recommendation (I will keep that in mind in the future) and thanks for the link, it worked for me! :) – Mephasm Vortex Mar 29 '15 at 19:42
  • Sure, I should probably specify that my image is a blob in my question too. – Mephasm Vortex Mar 29 '15 at 19:51
  • possible duplicate of [How to display multiples images from MySql](http://stackoverflow.com/questions/29329712/how-to-display-multiples-images-from-mysql) – Charkan Mar 29 '15 at 20:05
  • Vote Up requires 15 reputation (which I don't have – Mephasm Vortex Mar 30 '15 at 14:08
  • @MephasmVortex PLEASE UP-VOTE THE ANSWER. THANKS – Alive to die - Anant Feb 03 '16 at 06:35

2 Answers2

0

are u sure that getThumb.php has good file location?

<img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" />
  • Can you elaborate? Should getThump.php be in same directory as my php file that is handling the html formatting? – Mephasm Vortex Mar 29 '15 at 18:29
  • yes it will be in same working directory. if not then try to give full path of getThump.php – Alive to die - Anant Mar 29 '15 at 18:34
  • It was in the same directory. Also I just tried the absolute path for getThump.php and it's still the same. Is there any problem in the code maybe? Or my php settings? – Mephasm Vortex Mar 29 '15 at 18:39
  • what is $row['thumbnail'].... is that path of file like http://example.com/image/image.jpg or it is base64 encoded content? if it is first option, ju mast do echo file_get_contents($row['thumbnail']). – Rastislav Struhár Mar 29 '15 at 18:57
  • It is the second option, $row['thumbnail'] is a blob in my mysql db (that contains an uploaded jpeg image directly from phpmyadmin). – Mephasm Vortex Mar 29 '15 at 19:05
0

There are following problems may be in your code:-

  1. your image path is not correct (what you are saving in your database).

  2. Either you are saving your image directly with blob data-type and at the time retrieval you face some problem.

For the first problem try to hardcode path of your image and check, if working then your path saving to databsae is not correct and you have to correct.

For second problem please check this link:PHP display image BLOB from MySQL. It will definitely help you. Thanks.

Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98