繁体   English   中英

如何使用php显示数据库中的图像

[英]How to display image from database using php

我正在尝试显示来自数据库的图像,但我无法显示该图像。但是它的显示方式是user-1.jpg请看我的代码可以指导我如何显示图像。

$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows = mysql_fetch_assoc($imageresult1))
{       
    $image = $rows['image'];    
    print $image;
}

显示来自 MySql Db 的图像。

$db = mysqli_connect("localhost","root","","DbName"); 
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

我正在尝试显示来自数据库的图像,但我无法显示该图像。但是它的显示方式是这样的user-1.jpg请看我的代码可以指导我如何显示图像。

$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows = mysql_fetch_assoc($imageresult1))
{       
    $image = $rows['image'];    
    print $image;
}

例如,如果您使用此代码,则可以从 db (mysql) 加载图像并将其显示在 php5 中;)

<?php
   $con =mysql_connect("localhost", "root" , "");
   $sdb= mysql_select_db("my_database",$con);
   $sql = "SELECT * FROM `news` WHERE 1";
   $mq = mysql_query($sql) or die ("not working query");
   $row = mysql_fetch_array($mq) or die("line 44 not working");
   $s=$row['photo'];
   echo $row['photo'];

   echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:128px;height:128px">';
   ?>

您需要这样做才能显示图像

$sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows=mysql_fetch_assoc($imageresult1))
{
    $image = $rows['image'];
    echo "<img src='$image' >";
    echo "<br>";
} 

您需要使用 html img 标签。

$sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
    $imageresult1 = mysqli_query($link, $sqlimage);

    while($rows=mysqli_fetch_assoc($imageresult1))
{

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


} 
<?php
       $connection =mysql_connect("localhost", "root" , "");
       $sqlimage = "SELECT * FROM userdetail where `id` = '".$id1."'";
      $imageresult1 = mysql_query($sqlimage,$connection);

      while($rows = mysql_fetch_assoc($imageresult1))
    {       
       echo'<img height="300" width="300" src="data:image;base64,'.$rows['image'].'">';
    }
    ?>

把你$image放在 html 的img标签中

尝试这个

echo '<img src="your_path_to_image/'.$image.'" />';

代替

print $image;

your_path_to_image 将是您的图像文件夹的绝对路径,例如: /home/son/public_html/images/或作为您在服务器上的文件夹结构。

更新2:

如果您的图像位于此页面文件所在的同一文件夹中
你可以使用这个

echo '<img src="'.$image.'" />';

只需更换

print $image;

 echo '<img src=".$image." >';

而不是print $image; 你应该去print "<img src=<?$image;?>>"

并注意 $image 应包含图像的路径。

因此,如果您仅将图像名称存储在数据库中,那么您必须将图像的完整路径存储在数据库中,例如 /root/user/Documents/image.jpeg。

<?php
    $conn = mysql_connect ("localhost:3306","root","");
    $db = mysql_select_db ("database_name", $conn);

    if(!$db) {
        echo mysql_error();
    }

    $q = "SELECT image FROM table_name where id=4";
    $r = mysql_query ("$q",$conn);
    if($r) {
         while($row = mysql_fetch_array($r)) {
            header ("Content-type: image/jpeg");       
    echo $row ["image"];
        }
    }else{
        echo mysql_error();
    }
    ?>

sometimes problem may  occures because of port number of mysql server is incoreect to avoid it just write port number with host name like this "localhost:3306" 
in case if you have installed two mysql servers on same system then write port according to that

in order to display any data from database please make sure following steps
1.proper connection with sql
2.select database
3.write query 
4.write correct table name inside the query
5.and last is traverse through data

将此代码放入您的php页面。

$sql = "SELECT * FROM userdetail";
$result = mysqli_query("connection ", $sql);

while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
    echo "<img src='images/".$row['image']."'>";
    echo "<p>".$row['text']. "</p>";
}

我希望这是工作。

暂无
暂无

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

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