无法在MySQL结果集上执行计算

时间:2014-03-10 16:07:47

标签: php mysql

我正在尝试计算每条记录的投票百分比。尽管每条记录的票数不同,但每条记录都是100%。为什么会这样?

$artistname = "SELECT * from voting";
$artistnameresults = mysql_query( $artistname ) or die( "Could not get video games " .mysql_error() );

for( $i = 0; $i < mysql_numrows( $artistnameresults ); $i++ ) {
    $data = mysql_fetch_array( $artistnameresults );

    echo "<div>". $data['artist'] ." has " . $data['votes'] ." votes ";

    if($total_votes > 0)
        $temp_votes = $data['votes']/$total_votes;
    else
        $temp_votes = 1;

    echo '( '.($temp_votes*100).'%)';
    echo "</div>\n";
}

1 个答案:

答案 0 :(得分:0)

你在$ total_votes中没有任何东西..

$total_votes = $data['whatever_total_votes_is_here'];
if($total_votes > 0)
        $temp_votes = $data['votes']/$total_votes;
    else
        $temp_votes = 1;

如果您没有计算$ data中的总票数,那么您将需要计算或计算出数字。

将其传递给total_votes,以便您可以对其进行检查