显示来自db的图像

时间:2018-06-07 15:57:05

标签: php image mysqli base64

我有一张桌子

tbl_image 
--------------
imgID(int)
imgName(varchar)
image(blob)

这是显示图像的代码:

<?php
     $query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
     $result = mysqli_query($conn, $query);
     while ($row = mysqli_fetch_array($result)){
          $imgName = $row['imgName'];
          echo '<div class="col-sm-3 gallery-grids-left">
                <div class="gallery-grid">   
                <a class="example-image-link" href="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"
                     data-lightbox="example-set"
                     data-title='.$imgName.'>
                <img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"/></a></div></div>';
                    }
                ?>

但我真的不喜欢使用echo '';所以我改为

<?php
    $query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_array($result)){
    $imgName = $row['imgName'];
    ?>
        <div class="col-sm-3 gallery-grids-left">
            <div class="gallery-grid">   
                <a class="example-image-link" data-lightbox="example-set"
                    href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>" 
                    data-title="<?php echo imgName;?>">
                    <?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
                </a>
            </div>
        </div>
<?php }
?>

它只显示图像,没有标题没有“图像块”,如此图像demo

Plz帮助我显示我的错误?以及如何解决它。 非常感谢,

3 个答案:

答案 0 :(得分:0)

您对变量$

错过了imgName

尝试以下代码:

<?php
    $query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_array($result)){
    $imgName = $row['imgName'];
    ?>
        <div class="col-sm-3 gallery-grids-left">
            <div class="gallery-grid">   
                <a class="example-image-link" data-lightbox="example-set"
                    href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>" 
                    data-title="<?php echo $imgName;?>">
                    <?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
                </a>
            </div>
        </div>
<?php }
?>

答案 1 :(得分:0)

首先,正如@Suyog指出的那样,您错过了$imgName变量上的data-title。其次,如果您正在简化代码以尽可能多地使用html - 请执行此操作并删除额外的内容。有这样的事情:

<?php
    $query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_array($result)){
    $imgName = $row['imgName'];
    ?>
        <div class="col-sm-3 gallery-grids-left">
            <div class="gallery-grid">   
                <a class="example-image-link" data-lightbox="example-set"
                    href='<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] );?>"/>'
                    data-title="<?php echo $imgName;?>">
                    <img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] ); ?>">
                </a>
            </div>
        </div>
<?php } ?>

除此之外,我强烈建议不要在数据库中存储二进制数据。这不是数据库的用途。存储图像是文件并在数据库中存储文件名。它会让你的生活更轻松。

答案 2 :(得分:0)

代码中的HTML错误:PHP脚本的错误绑定

<?php
    $query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_array($result)){
    $imgName = $row['imgName'];
    ?>
        <div class="col-sm-3 gallery-grids-left">
            <div class="gallery-grid">   
                <a class="example-image-link" data-lightbox="example-set"
                    href="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>" 
                    data-title="<?php echo $imgName;?>">
                    <img src="data:image/jpeg;base64,<?php echo base64_encode($row['image']) ?> />
                </a>
            </div>
        </div>
<?php }
?>