简体   繁体   中英

How to store and retrieve image from MySQL database and display it in html using Php?

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);

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);

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

<?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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM