$sql="select sum(num) as totalnum from $table where product='a' ";
$result=mysql_query($sql);
$obj=mysql_fetch_object($result);
$x=$obj->totalnum;
echo $x;

解决方案 »

  1.   

    谢谢楼上的,那我可不可以这样写呢?
    $sql="select sum(num) as totalnum from $table where product='a' ";
    $result=mysql_query($sql);
    $obj=mysql_fetch_object($result);
    $x=$obj["totalnum"]
    echo $x;
      

  2.   

    错了,是
    $sql="select sum(num) as totalnum from $table where product='a' ";
    $result=mysql_query($sql);
    $obj=mysql_fetch_array($result);
    $x=$obj["totalnum"]
    echo $x;
      

  3.   

    对.
    如果需要求每个类别的总合使用SELECT SUM( `num` ) AS totalnum FROM `ss` GROUP BY `name`$sql="SELECT SUM( `num` ) AS totalnum FROM `$table` GROUP BY `name`";
    $result=mysql_query($sql);for($i=0; $i<3; $i++) {
    $row=mysql_fetch_assoc($result);
    $x=$row['totalnum'];
    echo $x,"<br/>";
    }
      

  4.   

    对应有类别名的:
    $sql="SELECT `product`, SUM( `num` ) AS totalnum FROM `$table` GROUP BY `product`";
    $result=mysql_query($sql);for($i=0; $i<3; $i++) {
    $row=mysql_fetch_assoc($result);
    $x=$row['product'];
    $y=$row['totalnum'];
    echo $x,$y,"<br/>";
    }