シーズン終了日を変数としてこの関数に渡すと、次のエラーが発生します。警告:ゼロ除算
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%
前もって感謝します!