dongqie4402
2017-07-05 10:45 阅读 33
已采纳

使用表中的图像显示MySQL数据

I have set up a table that displays data from my SQL database using

 $sql = "SELECT company_id, stk_buy, stk_sell FROM price";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
       echo "<table><tr><th>ID</th><th>Company</th><th>Buying Price</th><th>Selling Price</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["company_id"]."</td><td>".$row["stk_buy"]."</td><td>".$row["stk_sell"]."</td></tr>";
    }
    echo "</table>";
   } else {
       echo "0 results";
   }

I want to have images display in the Company column, different ones in each row. Similar to: enter image description here Is there a possible way of displaying images with my current html table setup or do I have to fetch each row to display images?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    doushi5024 doushi5024 2017-07-05 11:00

    You need to fetch the image name from the database, after fetching the images you need to put it inside a <IMG src=""> tag.

    The source of the image considering the img as you mention in the comment.

    SQL: $sql = "SELECT company_id, stk_buy, stk_sell FROM price";

    echo "<tr><td>".$row["company_id"]."</td><td><img src='img/".$row[company_id].".png' style='width:175px;height:75px;'/></td><td>".$row["stk_‌​buy"]."</td><td>".$r‌​ow["stk_sell"]."</td‌​></tr>";
    
    点赞 评论 复制链接分享
  • dongzhi6146 dongzhi6146 2017-07-05 10:55

    In these query you have not gave the image coloum name.You have to assign image coloum name like this

    $sql = "SELECT company_id,image(column name for image) stk_buy, stk_sell FROM price";
    
    点赞 评论 复制链接分享
  • dsa456369 dsa456369 2017-07-05 10:55

    First change your query to get image as well from table and then In your echo for table row, add one more td tag with img tag and src of the image to point the path where the image is stored. Something like

    echo "<tr><td>".$row["company_id"]."</td><td><img src='./path-to-image/".$row["image-column-name"]."' width='100' height='100'/></td><td>".$row["stk_buy"]."</td><td>".$row["stk_sell"]."</td></tr>";
    

    You can change width and height attributes value to what you want.

    点赞 评论 复制链接分享

相关推荐