【问题标题】:Inserting MySQL table data into a PHP array将 MySQL 表数据插入 PHP 数组
【发布时间】:2019-06-17 12:23:59
【问题描述】:

我一直在为我拥有的一家企业开发一个网站,但我对我的 PHP 和 SQL 有点生疏。我能够在 PHP 中创建我想要的模型商店/库存数组,但我一直在尝试将 MySQL 数据库中的表数据插入到我的 PHP 数组中。我想要做的是复制我的模型,但是,我不想从我的inventoryArray.php 中定义的数组中提取数据,而是从我在 MySQL 中创建的表中提取数据。

样机如下:

shop.php(当前):

<?php foreach ($inventory as $handgun) {?>
    <div class="column">
        <h5><?php echo "$handgun[model]"?></h5>
        <img class="thumbnail"
            src="assets/style/images/inventory/pistols/<?php echo "$handgun[img]" ?>.png">
        <table class="shopTables">
            <tr>
                <th>MPN:</th>
                <td><?php echo "$handgun[mpn]"?></td>
            </tr>
            <tr>
                <th>UPC:</th>
                <td><?php echo "$handgun[upc]"?></td>
            </tr>
            <tr>
                <th>Accessories:</th>
                <td><?php echo "$handgun[accessories]"?></td>
            </tr>
            <tr>
                <th>Description:</th>
                <td><?php echo "$handgun[description]"?></td>
            </tr>
        </table>
        <a href="product-page.php" class="button expanded">View</a>
    </div>
<?php } ?>

inventoryArray.php(之前):

    <?php
    $inventory = array(
         array(
             'action'        => "Striker-fired",
             'category'      => "Pistols",
             'cal_ga'        => "9mm",
             'manufacturer'  => "FN Herstal",
             'model'         => "FN 509 Midsize",
             'UPC'           => "845737010010",
             'img'           => "FN509M",
             'price'         => "$649"
         ),
         array(
             'action'        => "SA/DA",
             'category'      => "Pistols",
             'cal_ga'        => "9mm",
             'manufacturer'  => "CZ USA",
             'model'         => "CZ P01",
             'UPC'           => "806703911991",
             'img'           => "CZP01",
             'price'         => "$627"
         )
     );
    ?>

我没有更改 shop.php 的任何内容,以下是我尝试在我的 inventoryArray.php 文件中使用我的 MySQL 表中的数据:

inventoryArray.php(之后):

require_once ('../mysqli_connect.php');
$query = "SELECT * FROM firearms";

$response = @mysqli_query($dbc, $query);
while ($row= mysqli_fetch_array($response)) {
    $inventory = array(
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        )
    );
}

mysqli_close($dbc);

虽然这会将数据从我的表中插入到我的数组中,但将新库存插入到我的 SQL 表中不会创建类似于我的模型的新嵌套数组。相反,它似乎覆盖了原始数组。

【问题讨论】:

  • $inventory = array( 每次都会覆盖$inventory

标签: php mysql arrays database mysqli


【解决方案1】:

只需将行的关联数组提取到一个新数组中并动态追加到它:

while ($row = mysqli_fetch_assoc($response)) {
    $inventory[] = $row;
}

你真的可以这样做:

while ($inventory[] = mysqli_fetch_assoc($response));

如果表格中有更多列并且您不想要它们(没关系),那么只选择您想要的那些:

$query = "SELECT model, img, mpn, upc, accessories, description FROM firearms";

【讨论】:

  • 谢谢!完美地解决了我的需要:)
【解决方案2】:

它正在覆盖,因为您在每个循环上定义变量。而不是

while ($row= mysqli_fetch_array($response)) {
    $inventory = array(
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        )
    );
}

在循环之前定义$inventory,然后使用$inventory[]添加到它

$inventory = array();
while ($row= mysqli_fetch_array($response)) {
    $inventory[] =
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        );
}

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2014-12-02
    • 2011-11-10
    • 2011-04-27
    • 1970-01-01
    相关资源
    最近更新 更多