如何使用php根据mysql表中的数据自动创建div?

I want to create an ecommerce website, using PHP and mySQL DB. I have this code below and I want (with the use of php if possible), after connecting to sql DB(I did this) to repeat this DIV depending on the entries in the DB table and echo the data of each entry in table. I found this but I don't know if it's the right example: Automatically creating div's using data from a sql table

Here's the code:

<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<image>.jpg" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><Itemname></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"><Price></i></p>
        <p><i class="item_price"><Description></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="Itemname" /> 
            <input type="hidden" name="amount" value="Price"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

</div>

The example you provided above will work just fine, all you need to do is put the div/div's you want to generate multiple times inside a loop. then they will be generated as long as the loop condition is met.

But though the example u provided works but you need to note that it is using mysql_* functions which are depreciated and no longer supported by newer php versions .

Therefore I would advice that you learn about prepared statements instead, and use PDO.

consider the following :

<?php

    $sql = $pdo->query("SELECT * FROM products")->fetchall(PDO::FETCH_ASSOC);
    foreach($sql as $row):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><?php echo $row['item_name'];?></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
        <p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="<?php echo $row['item_name'];?>" /> 
            <input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

<?php

    endforeach;
?>

OR

<?php

    $sql = $pdo->query("SELECT * FROM products");
    while ($row = $sql->fetchall(PDO::FETCH_ASSOC)):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><?php echo $row['item_name'];?></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
        <p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="<?php echo $row['item_name'];?>" /> 
            <input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

<?php

    endwhile;
?>

Where $pdo is your database connection string.

This will fetch data and generates the div for each product.

If you not using prepared statements for your application, I would advice you to use them more especially pdo prepared statements, below are the places you can learn pdo prepared statements

https://phpdelusions.net/pdo#foreach

http://jayblanchard.net/demystifying_php_pdo.html