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

时间:2017-01-13 11:39:03

标签: php html mysql image

所以我有PHP代码来调用和显示mysql的产品.. 我有一个显示图片的问题..当用户将图片上传到数据库时,(图片中显示数据库)上传图片时,它存储在localhost和数据库中的文件夹中..它会自动创建一个要存储的图片的随机数..那么我该如何调用要查看的图片?在线:

img /是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();
?>
&#13;
<?php echo $dynamicList; ?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

    <?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();
    ?>

答案 1 :(得分:0)

您确定,img/1.jpg下有照片吗?您写道,图片中会有一个随机数,但正如我所看到的,您正在使用数据库主键标识符。