如何存储并从MySQL数据库中获取图像,并使用PHP在HTML显示呢?

问题描述 投票:1回答:3

我需要从MySQL数据库显示图像的列表,并显示图像。在这里我的示例代码其工作插入,但它并没有显示anything..can请人帮助我..

$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);
php jquery html mysql image
3个回答
0
投票

你做错了两两件事:

  1. 数据后缺少,:图像/ JPEG; BASE64
  2. 重新编码的base64数据

所以,这里是我的修正,试试这个:

...
echo '<img src="data:image/jpeg;base64,',base64_decode($row['imag']).'"/>';
fclose($file);

-2
投票

相反,在数据库中保存的二进制数据,尽量存放在数据库映像路径或文件名。在您的系统文件夹中存储的图像。这可能是更加更快

为了获取图像,只提取图片的路径或名称,并显示在你的HTML页面

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

在你的情况,如果ID是自动递增的话,不要将其插入。它会自动被插入


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

刚刚经历this article

© www.soinside.com 2019 - 2024. All rights reserved.