2

i need to display list of image from MySQL database and display the image. here my sample code its working for insertion but it does not display anything..can anyone please help me..

$file = fopen("switch.jpg", "rb");
$image = fread($file, filesize('switch.jpg'));
$image = base64_encode($img);
$ins_query="INSERT INTO mytable (id,imag) "."VALUES ('','$img')";
mysql_query($ins_query)or die('Error in query !');
$id1=1;
echo "inserted ";
 $query="select imag from mytable where id='$id1'";
     $result=mysql_query($query) or die("Error: ".mysql_error());
     $row=mysql_fetch_array($result);
     echo '<img src="data:image/jpeg;base64',base64_encode($row['imag']).'"/>';
fclose($file);
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
user3566029
  • 73
  • 3
  • 10
  • Your `INSERT` inserts a row with id set to '' (the empty string). Later you look for a row with `ID` of `1`, which won't exist unless you've created it some other way. –  May 02 '14 at 05:08
  • Store the url location of your image in database and import it in your html code – Nidhin S G May 02 '14 at 05:10
  • `$image = base64_encode($img);` - Where is `$img` defined? And you're using the same `$img` in your insert statement – asprin May 02 '14 at 05:49

3 Answers3

0

You're doing two things wrong:

  1. Missing , after data:image/jpeg;base64
  2. Re-encoding base64 data

So, here is my fix, try this:

...
echo '<img src="data:image/jpeg;base64,',base64_decode($row['imag']).'"/>';
fclose($file);
Vin.AI
  • 2,369
  • 2
  • 19
  • 40
-2

Instead of saving binary data in database , try to store image path or file name in database. Store image in your system folder. This could be much more faster

In order to fetch the image , just fetch the image path or name and show it in your html page

echo '<img src="<?php echo FULL_BASE_URL.'/'.$imagePathfromDB; ?>"/>';

In your case if ID is auto incremented then , don't insert it . it will automatically gets inserted

sshet
  • 1,152
  • 1
  • 6
  • 15
-2
<?php
$number_of_thumbs_in_row = 4;

        $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename,photo_category FROM gallery_photos");


            while( $row = mysql_fetch_array( $result ) )
            {
                $result_array[] = "<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/><br>$row[1]<br>$row[3]<br>$row[0]";

            }
            mysql_free_result( $result );   

            $result_final = "<tr valign='top' align='center' class='style1'>\n";

            foreach($result_array as $thumbnail_link)
            {

                if($counter == $number_of_thumbs_in_row)
                {   
                    $counter = 1;
                    $result_final .= "\n</tr align='center' class='style1'>\n<tr align='center' class='style1'>\n";

                }
                else

                $counter++;

                $result_final .= "\n<td class='style1'>".$thumbnail_link."</td>\n";


            }


            if($counter)
            {

                if($number_of_photos_in_row==$counter)
            $result_final .= "\n<td class='style1' colspan='".($number_of_photos_in_row=$counter)."'></td>\n";

                $result_final .= "</tr>";

            }

        }

echo <<<__HTML_END

<html>
<head>
    <title>Gallery View</title>
</head>
<body>
<table width='100%'  border='0' cellpadding="10" cellspacing="10">
$result_final   

</table>
</body>
</html>

__HTML_END;
?>

Just go through this article.

slavoo
  • 5,798
  • 64
  • 37
  • 39