doujiaohuo1096 2016-09-25 07:52
浏览 39
已采纳

如何将图像从mysql放到php表格单元格

I have this code:

<?php
 $images = 'image';
 $sql = "SELECT image FROM items WHERE itemsID = '4';";
 $result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>

<div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
<div class="wpmd">
<?php 
while($rows = mysqli_fetch_assoc($result)) { 
?>
<div><TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
</TD>
</TR>
</TABLE>
</DIV>
<?php 
}
?>
</DIV>
</DIV>

this should be work but not

I'm putting an image get from the database then put it into table cell but I keep getting this error

enter image description here

  • 写回答

1条回答 默认 最新

  • douhang1913 2016-09-25 08:42
    关注

    If you need to display the image from the mysql which is storing the image name that you have uploaded you have to follow the steps in order to display it.

    1. Ensure under which folder you have saved the uploaded image.
    2. Make sure that you store only the name of the image into the database so that we can retrieve it very easy by looping it over the query.

    Note: As per the question you have to retrieve the images from the database so that you need to follow the code which i have given below.

    Method 1: To retrieve the image from the DB

    Your code which displays the <?php $rows[$images];?> is wrong pertaining to that you need to display the name of the DB field over to there and echo the value and then alone you will be getting the image over there.

    Replace:

    <TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
    

    With:

    <TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>
    

    provided the $row['images'] where images is the field containing the name of the image stored in the DB. or else you can change the name as per the Table field what you have. Rather than that your code look good.

    Hence you entire code looks like,

    <?php
     $images = 'image';
     $sql = "SELECT image FROM items WHERE itemsID = '4';";
     $result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
    ?>
    <div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
      <div class="wpmd">
      <?php 
        while($rows = $result->fetch_assoc()) { 
        ?>
          <div>
            <TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
              <TR valign=top>
                <TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>
                </TD>
              </TR>
            </TABLE>
          </DIV>
        <?php 
        }
      ?>
      </DIV>
    </DIV>
    

    Method 2: To retrieve the image as per your Structure

    You have stored the $images='image'.

    Ensure that the table has the field image where it stores the image name. If it does so you can simply change the code like this .

    You are missing echo in the code so that it displays the name of the image stored in the DB and it prints that image stored under int images folder with the hep of the image name you given as input to the img tag.

    Replace:

    <TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
    

    with:

    <TD width=231 height=155><img src="images/<?php echo $rows[$images];?><BR>
    

    Note: But method 2 is not advisable since if you loop through the DB for getting the value you have to follow that Method-1 alone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算