【问题标题】:Why is my images not appearing, when trying to get them from database为什么我的图像没有出现,当试图从数据库中获取它们时
【发布时间】:2015-03-04 15:49:18
【问题描述】:

您好,我正在从我的数据库中获取信息以显示在网页上,由于信息显示在页面上,因此连接一切正常。

但是当涉及到图片时,他们会想出一个小盒子来说明它们应该在哪里。 这是我的代码:

<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>

<h2>Production</h2>

<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
      $Image = $rows['Image'];      
    while($row = $result->fetch_object()){
        echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
                ,$row->ProductionName,' ',$row->ProductionType, '</pre>';
    }
    $result->free();
    }
}
     echo $result; 

include './includes/footer.php';
?>

下面是屏幕上显示的图片。

【问题讨论】:

  • 右键单击这些图像并检查路径,或者只查看生成的 HTML。您很可能设置了不正确的图像路径
  • 它显然是路径,但我如何将它写成来自数据库,因为图片已经在那里?顺便说一句,它们是 blob 类型
  • @Lizzy 对图像的 HTML 调用将在您的 Web 服务器上查找图像文件。您需要将文件放在磁盘上的某个位置,以便可以找到它们。一种常见的做法是将图像存储在一个目录中,并将图像的路径存储在数据库中。
  • @BigScar Okies,谢谢,我如何在数据库中存储路径以及数据类型会变成什么?目前它们是我已将图片上传到其中的 blob。 :)
  • @Lizzy 您需要将图像存储在您的网络服务器上。然后在数据库中将每个图像的路径存储为文本字符串。

标签: php html image mysqli


【解决方案1】:

你需要在while循环中设置$Image,$rows['Image']应该是$row->Image,像这样。

<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){

    while($row = $result->fetch_object()){
        $Image = $row->Image;
        echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
                ,$row->ProductionName,' ',$row->ProductionType, '</pre>';
    }
    $result->free();
    }
}
     echo $result; 

include './includes/footer.php';
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 2011-06-24
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多