php 如何显示带有存储在数据库中的路径的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17875560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:37:26  来源:igfitidea点击:

How to Displaying an image with path stored in Database?

phphtmlmysqlimageimageview

提问by Taha Kirmani

I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.

我在数据库中存储了图像路径,在项目文件夹中存储了图像。现在我试图在我的 HTML 模板中查看所有带有产品的图像。我编写了以下代码,它在输出中给了我一个空图像框,当我在新选项卡中打开该图像框时,它会打开以下 URL。

 http://localhost/cms/1

Kindly tell me the way of referring images in 'img src ' tag.

请告诉我在'img src'标签中引用图像的方式。

enter image description here

在此处输入图片说明

include 'connect.php';

$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);


if (!$query=mysql_query ("select product_id,name from products")) {
    echo mysql_error();

} else {
    while ($query_array = mysql_fetch_array($query)) {
        echo '</br><pre>';

        $product_id = $query_array['product_id'];

        echo "<a href='single_product.php?product_id=$product_id' >";
        print_r ($query_array['name']);
        echo "</a>";

        echo "&lt;img src=". $image_query_fetch
        ['images'] ." alt=\"\" /&gt;";

        echo '</pre>';
    }
}

} else {
    echo "You need to Log in to visit this Page";

}

回答by Malaiyandi Murugan

Add your local image path before source

在源之前添加您的本地图像路径

example

例子

echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";

*Use PHP *

*使用 PHP *

<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

回答by Lars Juel Jensen

You can use the HTML base tag to set a base URL that relative URLs will be resolved from. See -> http://www.w3schools.com/tags/tag_base.asp

您可以使用 HTML 基本标签来设置一个基本 URL,相对 URL 将从中解析。见 -> http://www.w3schools.com/tags/tag_base.asp