[英]how to display image from mysql to php
I'm new to php and mysql. 我是php和mysql的新手。 I created databse in mysql, and I would also like to display my image from the database to my php site, but I only get a weird code (something like this: ?JFIF,, ), everything else works as it should.
我在mysql中创建了databse,我也想将数据库中的图像显示到我的php站点,但是我只得到一个奇怪的代码(像这样: ?JFIF ,,正如它应该。 This is my code:
这是我的代码:
<?php
$mysqli = new mysqli('localhost','root','','bas');
$result = $mysqli->query("SELECT * FROM bas1");
if($result->num_rows !=0)
{ while($rows = $result->fetch_assoc()
{$name=$rows['name'];
$price=$rows['price'];
$rate=$rows['rate'];
$image=$rows['image'];
echo "<tr>";
echo "<td>$name</td><td>$price</td><td>$rate</td><td>$image</td>";
echo "</tr>";}
} else {
echo "<tr>";
echo "<td>";
echo "no rusults";
echo "<td>";
echo "</tr>";}
?>
The image in databse is set to longblob. 数据库中的图像设置为longblob。 I would be very thankful if someone could help me
如果有人可以帮助我,我将非常感激
try this 尝试这个
use img tag
and encode the image 使用
img tag
并对图像进行编码
echo '<img src="data:image/jpeg;base64,\'.base64_encode( $image ).\'"/>';
I recommend you to not to store an image in the database you should store image somewhere in server directory and save the path of that image in the database. 我建议您不要将映像存储在数据库中,而应将映像存储在服务器目录中的某个位置,并将该映像的路径保存在数据库中。 and display image like this
并显示这样的图像
<img src="<?php echo $image?>">
and your image data type in your database should be text. 并且数据库中的图像数据类型应该是文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.