![](/img/trans.png)
[英]Trying to get the sum of all votes from my voting table but i get an error
[英]trying to count all votes from voting table but I get an error on line 153
致命錯誤:無法在第153行的/home/students/gar0349/public_html/project2voting.php中將stdClass類型的對象用作數組
<?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_object( $totalvotesresults );
echo "<div>Total number of votes is ". $data['total'] ."</div>\n";;
?>
$data = mysql_fetch_object( $totalvotesresults );
您正在獲取作為對象,因此您需要使用as
$data->total;
echo "<div>Total number of votes is ". $data->total ."</div>\n";;
$data
是一個對象,而不是數組。
嘗試:
echo "<div>Total number of votes is ". $data->total ."</div>\n";
<?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_object( $totalvotesresults );
echo "<div>Total number of votes is ". $data->total ."</div>\n";
?>
從第一行查詢中刪除第一個托架..和aslo
echo "<div>Total number of votes is ". $data->total ."</div>\n";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.