简体   繁体   中英

trying to count all votes from voting table but I get an error on line 153

Fatal error: Cannot use object of type stdClass as array in /home/students/gar0349/public_html/project2voting.php on line 153

<?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 );

you are fetching as object so you need to use as

$data->total;


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

$data is an object, not an array.
Try:

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";
?>

Remove The First Bracket From First Line QUery .. And aslo

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM