使用 PHP echo 从数据库中获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15242241/
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 08:52:41  来源:igfitidea点击:

Getting image out of database with PHP echo

phpmysql

提问by Awais Umar

I have a database in which there is a row containing the images. Now in my code, I want to get images out of the database, but somehow I am unable to do it. Following is my code – kindly help me out –:

我有一个数据库,其中有一行包含图像。现在在我的代码中,我想从数据库中获取图像,但不知何故我无法做到。以下是我的代码——请帮助我——:

<?php

$qry = mysql_query("SELECT * FROM products ORDER BY products.id DESC LIMIT 0, 1", $con);
if (!$qry)
{
    die("Query Failed: ". mysql_error());
}

while ($row = mysql_fetch_array($qry))
{
    echo "<h2>".$row['title']."</h2>";
    echo "<img src=".'Image/'.$row['image']." />";
    echo "<p>".substr($row['body'],0,200)."<a href=articles.php?id=".$row['id']." > Read more</a></p>";
    echo "<p>".$row['price']."</p>";
}

?>

If I don't use PHP and just use simply the <img>tag, then the path must be src="Image/passbook.jpg"and it works fine but it's not working with PHP. I am creating an admin panel so that the client can delete or update the images as he want, so I must not use simple <img>tag.

如果我不使用 PHP 而只使用<img>标记,那么路径必须是 src="Image/passbook.jpg"并且它可以正常工作但它不适用于 PHP。我正在创建一个管理面板,以便客户可以根据需要删除或更新图像,所以我不能使用简单的<img>标签。

采纳答案by Rikesh

Try changing,

尝试改变,

echo "<img src=".'Image/'.$row['image']." />";

to

echo "<img src='Image/".$row['image']."' />";

回答by Dipesh Parmar

You have error in image declaration code. Correct it as below,

您在图像声明代码中有错误。改正如下,

while( $row = mysql_fetch_array($qry) )
{
    echo "<h2>".$row['title']."</h2>";
    echo "<img src = 'Image/".$row['image']."' alt = ""/>";
    echo "<p>".substr($row['body'],0,200)."<a href=articles.php?id=".$row['id']." > Read more</a></p>";
    echo "<p>".$row['price']."</p>";
}

回答by Nirmal Ram

Try this

尝试这个

echo "<img src='Image/".$row['image']."' />";

回答by tattvamasi

try this

尝试这个

while($row=mysql_fetch_array($qry))
{
    $title = $row['title'];
    $src = $row['image'];
    $whatever = $row['body'];
    echo "$title <br/><img src="$src" alt="my fancy photo" height="" width=""/><br/>$watever";
}

回答by Ravinder Singh

Do this :

做这个 :

while($row = mysql_fetch_array($qry))
{
    echo "<h2>".$row['title']."</h2>";
    echo "<img src = 'Image/".$row['image']."' alt = ""/>";
    echo "<p>".substr($row['body'],0,200)."<a href=articles.php?id=".$row['id']." > Read more</a></p>";
    echo "<p>".$row['price']."</p>";
}

回答by Tapas Pal

Just use this

就用这个

echo "<img src=Image/".$row['image']." />";