我在以下设置中使用 Smarty:
$smarty = new Smarty;
$smarty -> caching =3600;
$smarty -> compile_check =true;
$smarty -> compile_dir = 'theme/compile/';
$smarty -> config_dir = 'theme/libs/';
$smarty -> cache_dir = 'theme/cache/';
$smarty -> plugins_dir = 'theme/libs/plugins/';
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';
$smarty -> clear_compiled_tpl();
我想用这个功能编写一个简单的访客计数器:
function counter() {
$ip = $_SERVER['REMOTE_ADDR'];
$now = time();
$y1 = jgmdate("Y ", $now);
$y1 = (int) $y1;
$m1 = jgmdate("m", $now);
$m1 = (int) $m1;
$d1 = jgmdate("d", $now);
$d1 = (int) $d1;
$result3 = mysql_query("SELECT `times`,`id` FROM `stat_ip` where `IP`='$ip' AND `year`='$y1' AND `month`='$m1' AND `day`='$d1' ;");
unset($n3);
$n3 = (int) mysql_num_rows($result3);
echo $n3;
if ($n3 == 0) {
mysql_query("INSERT INTO `stat_ip` (`id` ,`IP` ,`year` ,`month` ,`day`) VALUES (NULL , '$ip', '$y1', '$m1', '$d1') ;");
} else if ($n3 == 1) {
$row3 = mysql_fetch_array($result3);
$s = (int) $row3['times'] + 1;
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;");
} else {
echo("error");
}
}
一切正常,但我的查询在这一行中执行了不止一次(可能是所有查询):
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;") ;
我认为聪明与我的问题有关!
当我写这段代码时:
$q=$smarty -> fetch('index.tpl');
查询执行 1 次,但是当我将代码更改为:
$q=$smarty -> fetch('index.tpl');
echo $q;
或者
$smarty -> display('index.tpl');
我的查询执行了不止一次!:(
了解更多信息: