使用PHP显示多张图片

3

我需要从具有特定ID的数据库表中显示多个图像,但会出现错误的图像(像这样)

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Display multiple images from MySQL database in PHP</title>
</head>
<body>
  <?php
    $tag_id=111;

    $link = mysqli_connect("localhost", "root", "mysql", "hospital");
    $file_path = 'http://localhost/display-all-images.php';

    $sql = "SELECT * FROM `rays_analysis` WHERE id=$tag_id;";
    $mq = mysqli_query( $link, $sql) or die ("not working query");

    while ($all_images = $mq->fetch_assoc())
    {
      $image = htmlspecialchars(stripslashes($all_images["id"]));
      // $image = $all_images ['id'];

      echo '<img src="http://localhost/display-all-images.php/'.$image.'" width="360" height="150">';
    }
  ?>
</body>
</html>

1
这不是正确的方式:http://localhost/display-all-images.php/'.$image。我的意思是你在 PHP 文件中找到图片? - Maninderpreet Singh
1个回答

4
您需要正确的图像路径。
echo '<img src="http://localhost/display-all-images.php/'.$image.'" width="360" height="150">';

在您的代码中,您发现图片是在php文件中而不是文件夹中。如果您的$image变量类似于imagename.jpg并且存储在images文件夹中。

您的语法应该是:

echo '<img src="http://localhost/yourprojectname/images/'.$image.'" width="360" height="150">';

$image变量返回什么?你能打印出来吗? - Maninderpreet Singh
我用完全不同的代码进行了更改,现在它可以正常工作 :) 非常感谢您的帮助 :) - بسمة صلاح

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接