【问题标题】:Displaying image from MySQL other than echoing显示来自 MySQL 的图像而不是回显
【发布时间】:2018-07-31 02:02:44
【问题描述】:

除了像这样回显它之外,还有其他方法可以将图像从 MySQL 放入 html:

  echo "<img src='images/" . $row['event_image'] . "' height='200' width='320' class=responsivez> ";

我试图将图像路径放在 html 标记上。 getImage.php 显示我的图像路径,但在下一页调用时图像不显示。我错过了一些重要的语法吗?

<img src="images/getImage.php?" width="175" height="200" />

getImage.php

<?php
$con=mysqli_connect("localhost","root","","blast");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
} 
$state='Johor';
$result=mysqli_query($con,"SELECT * from blast_events where 
event_state='$state'");

if (!$result) {
die ('SQL Error: ' . mysqli_error($con));
}
else{    
}
$row=mysqli_fetch_array($result);

echo $row["event_image"];
?>

【问题讨论】:

  • 执行 cmd+u(在 Mac 上)或 ctrl+u(在 Windows 上),并查看 src 属性中的链接正确与否。
  • 您正在以完全正确的方式进行此操作(除了参数化);回显&lt;img&gt; 标签。话虽如此,$row['event_image'] 不应该给你getImage.php?。是在你的表格行吗?你应该存储类似image001.png 的东西。然后您的&lt;img&gt; 标签将正确映射(假设相关图像在服务器上)。
  • @ObsidianAge 是的。 event_image 是数据库上的图像列名称。 getImage.php 显示图像的名称,.png

标签: php html image mysqli


【解决方案1】:

尝试输出图像而不仅仅是路径:

   header('Content-Type:image/jpeg');
   readfile($row["event_image"]); // make sure the path is valid not only filename.
// echo $row["event_image"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多