gpt4 book ai didi

php - 如何使用 PHP 错误显示 MySql 查询的多个结果

转载 作者:行者123 更新时间:2023-11-29 13:09:03 25 4
gpt4 key购买 nike

我有一个商品类别查询,在本例中我有 2 种来自智利的 Wine 。我下面的代码工作正常,但只有 foreach 循环或无论它是什么,它会将第一个输出与第二个输出重叠。

我是 for-each 循环的新手

这是我的 PHP:

<?php  

include"db_connection.php";

$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");

$allRows = array();
while($row = mysql_fetch_array($sql)) {
$allRows[] = $row;
}


foreach ($allRows as $row) {
$id = $row ["id"];
$description = $row["description"];
$wine_type = $row["wine_type"];
$country = $row["country"];
$bottle_price = $row["bottle_price"];
$indicator = $row["indicator"];
$colour = $row["colour"];
$case_price = $row["case_price"];
$case_size = $row["case_size"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

}


?>

这是 HTML:

<?php include('header.php'); ?>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<?php
foreach ($allRows as $row) {
?>
<tr>
<td width="19%" valign="top"><img src="inventory_images/<?php echo $row['id']; ?>.jpg" width="142" height="188" alt="<?php echo $row['wine_type']; ?>" /><br />
<a href="inventory_images/<?php echo $id; ?>.jpg">View Full Size Image</a></td>
<td width="81%" valign="top"><h3><?php echo $wine_type; ?></h3>
<p><?php echo "$".$bottle_price; ?><br /><br />
<?php echo "$country $indicator"; ?> <br /><br />
<?php echo $description; ?> <br />
</p>

<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
<?php include('footer.php'); ?>

最佳答案

发生的情况是您覆盖了您的 $id等每个循环上的变量,因此只有最后一行的结果被保存在那里。

但是,您根本不需要这些变量,因为您已将所有信息保存在 $allRows 中。 。就像你通过<?php echo $row['id']; ?>访问id一样在 HTML 中,您应该处理所有其他变量。例如:$row["bottle_price"]而不是$bottle_price .

您的 PHP 代码将如下所示:

<?php 
include"db_connection.php";

$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");
$allRows = array();

while($row = mysql_fetch_array($sql)) {
$allRows[] = $row;
}
?>

你的 HTML 像这样:

<?php include('header.php'); ?>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<?php
foreach ($allRows as $row) {
?>
<tr>
<td width="19%" valign="top">
<img src="inventory_images/<?php echo $row['id']; ?>.jpg" width="142" height="188" alt="<?php echo $row['wine_type']; ?>" /><br />
<a href="inventory_images/<?php echo $row['id']; ?>.jpg">View Full Size Image</a>
</td>
<td width="81%" valign="top">
<h3><?php echo $row['wine_type']; ?></h3>
<p><?php echo "$".$row['bottle_price']; ?><br /><br />
<?php echo $row['country']." ".$row['indicator']; ?> <br /><br />
<?php echo $row['description']']; ?> <br />
</p>

<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $row['id']; ?>" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
<?php include('footer.php'); ?>

关于php - 如何使用 PHP 错误显示 MySql 查询的多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22328866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com