【问题标题】:How to store and retrieve image from MySQL database and display it in html using Php? [duplicate]如何从 MySQL 数据库中存储和检索图像并使用 Php 将其显示在 html 中? [复制]
【发布时间】:2014-06-18 17:22:10
【问题描述】:

我需要显示来自 MySQL 数据库的图像列表并显示图像。这里我的示例代码可以插入,但它不显示任何内容..任何人都可以帮助我..

$file = fopen("switch.jpg", "rb");
$image = fread($file, filesize('switch.jpg'));
$image = base64_encode($img);
$ins_query="INSERT INTO mytable (id,imag) "."VALUES ('','$img')";
mysql_query($ins_query)or die('Error in query !');
$id1=1;
echo "inserted ";
 $query="select imag from mytable where id='$id1'";
     $result=mysql_query($query) or die("Error: ".mysql_error());
     $row=mysql_fetch_array($result);
     echo '<img src="data:image/jpeg;base64',base64_encode($row['imag']).'"/>';
fclose($file);

【问题讨论】:

  • 您的 INSERT 插入一行,其 id 设置为 ''(空字符串)。稍后您会查找 ID1 的行,除非您以其他方式创建它,否则它不会存在。
  • 将图片的url位置存储在数据库中并导入到您的html代码中
  • $image = base64_encode($img); - $img 在哪里定义?而且您在插入语句中使用了相同的$img

标签: php jquery html mysql image


【解决方案1】:

不要将二进制数据保存在数据库中,而是尝试将图像路径或文件名存储在数据库中。 将图像存储在系统文件夹中。这可能会更快

为了获取图像,只需获取图像路径或名称并将其显示在您的 html 页面中

echo '<img src="<?php echo FULL_BASE_URL.'/'.$imagePathfromDB; ?>"/>';

在您的情况下,如果 ID 是自动递增的,请不要插入它。它会自动插入

【讨论】:

    【解决方案2】:
    <?php
    $number_of_thumbs_in_row = 4;
    
            $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename,photo_category FROM gallery_photos");
    
    
                while( $row = mysql_fetch_array( $result ) )
                {
                    $result_array[] = "<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/><br>$row[1]<br>$row[3]<br>$row[0]";
    
                }
                mysql_free_result( $result );   
    
                $result_final = "<tr valign='top' align='center' class='style1'>\n";
    
                foreach($result_array as $thumbnail_link)
                {
    
                    if($counter == $number_of_thumbs_in_row)
                    {   
                        $counter = 1;
                        $result_final .= "\n</tr align='center' class='style1'>\n<tr align='center' class='style1'>\n";
    
                    }
                    else
    
                    $counter++;
    
                    $result_final .= "\n<td class='style1'>".$thumbnail_link."</td>\n";
    
    
                }
    
    
                if($counter)
                {
    
                    if($number_of_photos_in_row==$counter)
                $result_final .= "\n<td class='style1' colspan='".($number_of_photos_in_row=$counter)."'></td>\n";
    
                    $result_final .= "</tr>";
    
                }
    
            }
    
    echo <<<__HTML_END
    
    <html>
    <head>
        <title>Gallery View</title>
    </head>
    <body>
    <table width='100%'  border='0' cellpadding="10" cellspacing="10">
    $result_final   
    
    </table>
    </body>
    </html>
    
    __HTML_END;
    ?>
    

    只需通过this article

    【讨论】:

      【解决方案3】:

      你做错了两件事:

      1. data:image/jpeg;base64 之后缺少 ,
      2. 重新编码 base64 数据

      所以,这是我的解决方法,试试这个:

      ...
      echo '<img src="data:image/jpeg;base64,',base64_decode($row['imag']).'"/>';
      fclose($file);
      

      【讨论】:

        猜你喜欢
        • 2010-12-10
        • 2011-12-09
        • 2013-01-13
        • 1970-01-01
        • 2013-05-02
        • 2014-02-28
        • 2014-06-15
        相关资源
        最近更新 更多