dounuogong0358
dounuogong0358
2017-01-13 11:39

如何在php中显示来自mysql的图像

已采纳

So i have the PHP code to call and display the product from mysql.. i have a problem with displaying the picture.. when a user add the item to database, (database as shown in picture) when the picture is uploaded, its store in a folder in localhost and on the database.. it will auto create a random number for the picture to store.. so how should i call the picture to be viewed? in line:

img/ is the folder in localhost.

DATABASE EXP:

enter image description here

<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "dbconnect.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM product ORDER BY proDate DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $proID = $row["proID"];
             $proName = $row["proName"];
             $proPrice = $row["proPrice"];
             $proDate = strftime("%b %d, %Y", strtotime($row["proDate"]));
             $dynamicList .= '
      
                            <div class="single-product">
                                <div class="product-f-image">
                                    <img src="img/' . $proID . '.jpg" alt="">
                                    <div class="product-hover">
                                    <a href="#" class="add-to-cart-link"><i class="fa fa-shopping-cart"></i> Add to cart</a>
                                    <a href="single-product.php?id=' . $proID . '" class="view-details-link"><i class="fa fa-link"></i> See details</a>
                                    </div>
                                </div>
                                
                            <h2>' . $proName . '</h2>

                                <div class="product-carousel-price">
                                <ins>$' . $proPrice . '</ins> <del>$425.00</del>
                                </div>                                 
                            </div>
      ';
      
    }
} else {
    $dynamicList = "no new products";
}
mysql_close();
?>
<?php echo $dynamicList; ?>

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • drutcs7210 drutcs7210 4年前
        <?php 
        // Run a select query to get my letest 6 items
        // Connect to the MySQL database  
        include "dbconnect.php"; 
        $dynamicList = "";
        $sql = mysql_query("SELECT * FROM product ORDER BY proDate DESC LIMIT 6");
        $productCount = mysql_num_rows($sql); // count the output amount
        if ($productCount > 0) {
            while($row = mysql_fetch_array($sql)){ 
                     $proID = $row["proID"];
                     $proImg = $row["proImg"];
                     $proName = $row["proName"];
                     $proPrice = $row["proPrice"];
                     $proDate = strftime("%b %d, %Y", strtotime($row["proDate"]));
                     $dynamicList .= '
    
                                    <div class="single-product">
                                        <div class="product-f-image">
                                            <img src="img/' . $proImg . '" alt="">
                                            <div class="product-hover">
                                            <a href="#" class="add-to-cart-link"><i class="fa fa-shopping-cart"></i> Add to cart</a>
                                            <a href="single-product.php?id=' . $proID . '" class="view-details-link"><i class="fa fa-link"></i> See details</a>
                                            </div>
                                        </div>
    
                                    <h2>' . $proName . '</h2>
    
                                        <div class="product-carousel-price">
                                        <ins>$' . $proPrice . '</ins> <del>$425.00</del>
                                        </div>                                 
                                    </div>
              ';
    
            }
        } else {
            $dynamicList = "no new products";
        }
        mysql_close();
        ?>
    
    点赞 评论 复制链接分享
  • doutang3815 doutang3815 4年前

    Are you sure, that under img/1.jpg, there is a picture? You wrote that there will be a random number for the picture, but as I can see, You are using database primary key identifier.

    点赞 评论 复制链接分享

相关推荐