为什么我的ForEach循环只返回一行?

时间:2015-10-19 02:55:17

标签: php mysqli

<!doctype html>
<?php
?>
<html>
    <head>
        <title>Midterm Review</title>
    </head>
    <body>
        <h3>Tools Not Currently in Stock</h3>
        <?php
            $conn = mysqli_connect(This part works );
            mysqli_select_db(this part works);
            $query = "SELECT * FROM `midterm` WHERE stock='0'";
            $result = mysqli_query($conn, $query);

            echo '<table>';
            echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
            foreach ($result as $row) {
                $row = mysqli_fetch_array($result);
                echo '<tr>';
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['part_number'] . '</td>';
                echo '<td>' . $row['description'] . '</td>';
                echo '<td>' . $row['stock'] . '</td>';
                echo '<td>' . $row['price'] . '</td>';
                echo '<td>' . $row['received_date'] . '</td>';
                echo '</tr>';
                echo '</table>';
            };
            mysqli_close($conn);

        ?>
        <!--<form method="post" action="midterm_confirmation.php">
            <label>Part Number: </label><input type="text" name="partNumber" /><br />
            <label>Description: </label><input type="text" name="description" /></br />
            <label>Stock: </label><input type="number" name="stock" /><br />
            <label>Price: </label><input type="text" name ="price" /></br />
            <label>Received Date: </label><input type="text" name="receivedDate" /></br />
            <input type="Submit" value="Add to Stock">
            </form> -->
    </body>
</html>

基本上我的最终结果是我得到一个表行,而不是我得到的两个。有什么建议?我已经填充了所有的表,但只运行了一次,而不是发布我库存等于零的每一行。

3 个答案:

答案 0 :(得分:2)

请参阅下面的代码段。附:不要混合模板和数据库层,这是一个难闻的气味......

<?php
// establish connection to $conn variable

$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['part_number'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['stock'] . '</td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '<td>' . $row['received_date'] . '</td>';
    echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>

答案 1 :(得分:1)

问题在于关闭</table>它应该保持在循环之外。

while ($row = mysqli_fetch_assoc($query)) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['part_number'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['stock'] . '</td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '<td>' . $row['received_date'] . '</td>';
    echo '</tr>';
    // echo '</table>';// this is wrong.
};
echo '</table>';// this is correct. closing table inside loop is wrong, do it outside the loop.

答案 2 :(得分:0)

您可以使用while循环执行此操作,如下所示:

<?php
// establish connection to $conn    
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['part_number'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['stock'] . '</td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '<td>' . $row['received_date'] . '</td>';
    echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>

或者如果你想使用foreach,那么为它写一个额外的函数:

<?php
function mysql_fetch_all($result) {
   $rows = array();
   while ($row = mysql_fetch_array($result)) {
     $rows[] = $row;
   }
   return $rows;
}


// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach (mysql_fetch_all($result) as $row){
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['part_number'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['stock'] . '</td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '<td>' . $row['received_date'] . '</td>';
    echo '</tr>';
} 
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>