如下sql中不要用mysql内置函数now()等,这样第一可以提高sql执行效率,第二统一程序层处理sql中时间参数,避免因服务器时间差导致问题产生。
使用PDO预处理,第一可以提高sql效率,第二可以避免sql注入问题。
$db->Execute("INSERT INTO `h5dfdh_lefttime_log`(`mobile`, `lefttime`, `channel`, `create_time`)VALUES(?, ?, ?, ?)", array($mobile, $initConfig['firstConferencePrize'], $initConfig['getLeftTimeChannel']['conference'], date("Y-m-d H:i:s")));
$last_invok_time = "2018-09-13 23:12:55";
$sth = $db->Execute("SELECT `channel`, sum(`lefttime`) as total FROM `h5dfdh_lefttime_log` where `mobile` = ? and `channel` = ? and `create_time` < ? and `create_time` > ? GROUP BY `channel`", array($mobile, $initConfig['getLeftTimeChannel']['share'], date("Y-m-d H:i:s"), $last_invok_time));