开发者

php count to count rows total

i want to count total number of rows, sample :

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

Total = 1, becouse 1+1+(-1)+1+(-1)=1

i want count it from database

my code is :

<?  $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
    }
 ?>

Thank you :)


Try...

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

example..

$sql = mysql_query("SELECT SUM(mood_num) FROM like_mood WHERE img_id= '$imageid'");
$sql = mysql_fetch_array($sql);
$total = $sql[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);

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜