Sql Queries executes more than a time with smarty
===

I am using Smarty with this settings:
    
    $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();
    

I want to program a simple visitors counter with this function :

    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') ;") ;
			
    }
    elseif($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");
    }


everything is ok , but my query executes more than a time in this line :

    mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;") ;


I think smarty has something to do with my problem!

