当我将赛季结束日期作为变量传递给此函数时,我收到此错误:警告:除以零
function win_percentage($season_start, $season_end)
{
$wins = mysql_query("SELECT COUNT(result) FROM scorecards WHERE result = 'Won' AND gamedate >= $season_start AND gamedate <= $season_end;");
$losses = mysql_query("SELECT COUNT(result) FROM scorecards WHERE result = 'Lost' AND gamedate >= $season_start AND gamedate <= $season_end;");
$winstotal = mysql_fetch_array($wins);
$lossestotal = mysql_fetch_array($losses);
$resultgames = $winstotal['COUNT(result)'] + $lossestotal['COUNT(result)'];
$winratio = ROUND(($winstotal['COUNT(result)'] / $resultgames),2) * 100;
return $winratio;
}
echo win_percentage('2012-01-01', '2012-09-10')
输出:警告:除以零
但是,当我将季节结束的日期值直接放在函数中时,它可以完美运行。这些与代码块之间的唯一区别是第 3 行和第 4 行的结尾,其他一切都相同。为什么我不能在不抛出错误的情况下将 season_end 传递给该函数?
function win_percentage($season_start, $season_end)
{
$wins = mysql_query("SELECT COUNT(result) FROM scorecards WHERE result = 'Won' AND gamedate >= $season_start AND gamedate <= '2012-12-31';");
$losses = mysql_query("SELECT COUNT(result) FROM scorecards WHERE result = 'Lost' AND gamedate >= $season_start AND gamedate <= '2012-12-31';");
$winstotal = mysql_fetch_array($wins);
$lossestotal = mysql_fetch_array($losses);
$resultgames = $winstotal['COUNT(result)'] + $lossestotal['COUNT(result)'];
$winratio = ROUND(($winstotal['COUNT(result)'] / $resultgames),2) * 100;
return $winratio;
}
echo win_percentage('2012-01-01', '2012-09-10')
输出:57%
提前致谢!