请帮助我让这个工作,抱歉,如果代码不干净,我只是PHP的初学者
<?php
$strSQL = "SELECT * FROM ps_product";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($prices3 = mysql_fetch_array($objQuery)) {
$total_price = $priceCalc;
?>
<?=$prices3["id_product"];?>=><? echo $total_price; ?>,
<? }; ?>
以上代码给出了以下结果(ID =&gt; Price):
3 =&gt; 55,4 =&gt; 28,5 =&gt; 35,
如何将结果添加到ARRY?
我希望这样做,为了看起来像:
$prices = array(
3=>55,
4=>28,
5=>35,
...
);
foreach ($prices as $id => $price) {
$query = "UPDATE ps_product_shop SET price='".$price."' WHERE
id_product='".$id."' ";
mysql_query($query);
}
答案 0 :(得分:1)
您可以像这样使用array_push
:
$result = array();
while($prices3 = mysql_fetch_array($objQuery)) {
$total_price = $priceCalc;
array_push($result, $total_price);
}
?>
答案 1 :(得分:1)
试试这个
$res_arr = array();
while($prices3 = mysql_fetch_array($objQuery)) {
$res_arr[$prices3["id_product"]] = $priceCalc;
}
print_r($res_arr);
答案 2 :(得分:0)
$i = 0;
$prices = array();
while($prices3 = mysql_fetch_array($objQuery)) {
$total_price = $priceCalc;
$prices[$i] = $total_price;
$i++;
}
?>