<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while ($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?>$</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td>
</tr>
<?php
}
?>
<tr>
<td>Total Price: <?php echo $totalprice ?></td>
</tr>
这是我目前使用的代码。 我得到的明显错误是:
致命错误:未捕获错误:调用未定义函数mysql_query()
我知道我需要将其更改为PDO,但我不知道在获得我想要的总价格的同时更换mysql_fetch_array
的内容。
将其更改为:
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$stmt = $db->prepare($sql);
$stmt->execute();
$totalprice=0;
我不确定如何继续获得正确的总价
答案 0 :(得分:0)
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$stmt = $db->prepare($sql);
$stmt->execute();
$totalprice=0;
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); # FOr mysql_fetch_assoc
foreach ($result as $index=>$arr) {
// $val = $arr['key'];
}
纯mysql方式:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC) {
// $val = $row['key'];
}
对于mysql_fetch__array:
您可以使用PDO :: FETCH_NUM
链接:http://php.net/manual/en/pdostatement.fetchall.php
http://micmap.org/php-by-example/manual/de/pdostatement.fetch.html