繁体   English   中英

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

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

我必须在mysql数据库的php网页上显示图像,并在其中给出图像的路径。 但它仅显示路径,而不显示图像。 我使用以下代码:

<?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;
 ?>

我得到的输出是:

图片/DSC01750.jpg

您可以帮我一些身体实际显示图像,而不显示路径。

非常感谢

您需要将其包装在img标签中。

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

显示多张图像

<?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\">";
}

 ?>

如果您希望在HTML页面中显示图像,则可以这样构造image标签:

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

如果您试图遮盖图像的位置并通过PHP脚本进行投放,则图像标签将如下所示:

<img src="yourscript.php" />

您可以修改脚本以打开并输出文件:

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

如果$row['Pics']包含图像的路径,则需要将其转换为图像引用,以将浏览器定向到该路径。 像这样:

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

注意,“路径”在这里可能意味着完全不同的东西。 如果它是服务器端的标准路径,则需要将其转换为浏览器可用的相对于应用程序的路径。 /usr/web/something/blah/开头的内容对浏览器无用。

暂无
暂无

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

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