问题是由于这些变量的作用域。当您尝试在while循环之外回显那些变量时;PHP在循环内创建(和分配)变量时找不到它们。为了解决这个问题,也可以给两个外部变量都赋一个空白值:
if(!$r) echo mysql_error();$rat = 0;$v = 1; // In case there are no records.while($row=mysql_fetch_array($r)){ $v = $row['total_votes']; $tv = $row['total_value']; $rat = $tv/$v;}


