试图计算投票表中的所有投票,但我在第153行收到错误

时间:2014-03-10 14:03:53

标签: php

致命错误:不能在第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";;
?>

3 个答案:

答案 0 :(得分:1)

$data = mysql_fetch_object( $totalvotesresults );

您正在获取对象,因此您需要使用

$data->total;


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

答案 1 :(得分:0)

$data是一个对象,而不是一个数组 尝试:

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

答案 2 :(得分:0)

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

从第一行删除第一个支架QUery ..并且aslo

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