简体   繁体   English

如何在php中从mysql显示网页上的图像

[英]how to display image on web page from mysql in php

I have to display images on my php web page from mysql database where i have give the path of my images. 我必须在mysql数据库的php网页上显示图像,并在其中给出图像的路径。 but Its only displaying the path but not the image. 但它仅显示路径,而不显示图像。 I used the following Code: 我使用以下代码:

<?php
    $servername="localhost";
    $username="root";
    $conn= mysql_connect($servername,$username)or die(mysql_error());
    mysql_select_db("ek",$conn);//EK is my Database
    $sql="select Pics from images";
    $result=mysql_query($sql,$conn) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    $image = $row['Pics'];
    print $image;
 ?>

and the output I am getting is : 我得到的输出是:

images/DSC01750.jpg 图片/DSC01750.jpg

Some body can you please help me to actually displaying the image but not the path. 您可以帮我一些身体实际显示图像,而不显示路径。

Many Thanks 非常感谢

You need to wrap it in an img tag. 您需要将其包装在img标签中。

echo "<img src=\"$image\">";

To display several images 显示多张图像

<?php

$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("ek",$conn);//EK is my Database
$sql="select Pics from images";
$result=mysql_query($sql,$conn) or die(mysql_error());
$i=0; 
$display_num =4;
while($row = mysql_fetch_assoc($result) && $i++ < $display_num){
  $image = $row['Pics'];
  echo "<img src=\"$image\">";
}

 ?>

If you wish to display an image in an HTML page, you can construct the image tag like this: 如果您希望在HTML页面中显示图像,则可以这样构造image标签:

<img src="<?php echo $image; ?>" />

If you are attempting to obscure the location of an image and serve it via the PHP script, your image tag will look something like this: 如果您试图遮盖图像的位置并通过PHP脚本进行投放,则图像标签将如下所示:

<img src="yourscript.php" />

and you can modify your script to open and output the file: 您可以修改脚本以打开并输出文件:

$image = $row['Pics'];
header("Content-Type: image/jpg");
readfile( $image ); 

If $row['Pics'] contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. 如果$row['Pics']包含图像的路径,则需要将其转换为图像引用,以将浏览器定向到该路径。 Something like this: 像这样:

<img src="<?php echo $image; ?>" />

Note that "path" can mean something entirely different here. 注意,“路径”在这里可能意味着完全不同的东西。 If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. 如果它是服务器端的标准路径,则需要将其转换为浏览器可用的相对于应用程序的路径。 Something that begins with /usr/web/something/blah/ won't be useful to the browser. /usr/web/something/blah/开头的内容对浏览器无用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM