php计数总计行数

时间:2011-04-07 23:28:41

标签: php count

我想计算总行数,样本:

row1 = 1
row2 = 1
row3 = -1
row4 = 1
row5 = -1

总计= 1,因为1 + 1 +( - 1)+1 +( - 1)= 1

我想从数据库中计算

我的代码是:

<?  $result_count_like = mysql_query("SELECT * FROM like_mood WHERE img_id='$imageid'");
while($row_like_co = mysql_fetch_array($result_count_like))
  {
  $total_num=$row_like_co['mood_num']; // THIS IS ROWS NAME
    }
 ?>

谢谢:)

2 个答案:

答案 0 :(得分:2)

...试

SELECT SUM(mood_num) FROM like_mood WHERE img_id= '$imageid'

例如..

$sql = mysql_query("SELECT SUM(mood_num) FROM like_mood WHERE img_id= '$imageid'");
$sql = mysql_fetch_array($sql);
$total = $sql[0];

答案 1 :(得分:0)

<?
$toCount = array();
$result_count_like = mysql_query("SELECT * FROM like_mood WHERE img_id='$imageid'");
while($row_like_co = mysql_fetch_array($result_count_like))
{
  $toCount[]=$row_like_co['mood_num']; // THIS IS ROWS NAME
}

echo array_sum($toCount);

?>