试图从我的投票表中得到所有投票的总和,但我得到一个错误

时间:2014-03-10 13:42:09

标签: php mysql sql

<?php

$totalvotes = ("SELECT COUNT(*) AS total FROM voting WHERE votes >= 0 ");

    $totalvotesresults = mysql_query( $totalvotes )
or die( "Could not get total votes " .mysql_error() );

$data = mysql_fetch_array( $totalvotesresults );
echo "<div>Total number of votes is ". $data['votes'] ."</div>\n";;
?>

1 个答案:

答案 0 :(得分:4)

您调用COUNT(*)查询total的结果,但在PHP中使用votes

echo "<div>Total number of votes is ". $data['votes'] ."</div>\n";

应该是:

echo "<div>Total number of votes is ". $data['total'] ."</div>\n";

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial