首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用smarty多次执行Sql查询

问使用smarty多次执行Sql查询
EN

Stack Overflow用户
提问于 2012-07-16 17:32:42
回答 4查看 1.1K关注 0票数 0

我正在使用带有以下设置的Smarty:

代码语言:javascript
复制
$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();

我想用这个函数编写一个简单的访客计数器:

代码语言:javascript
复制
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");
    }
}

一切正常,但是我的查询在这一行中执行了不止一次(可能是所有查询):

代码语言:javascript
复制
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;") ;

我觉得聪明跟我的问题有关!

当我写这段代码时:

代码语言:javascript
复制
$q=$smarty -> fetch('index.tpl');

但是,当我将代码更改为:

代码语言:javascript
复制
$q=$smarty -> fetch('index.tpl');
echo $q;

或

代码语言:javascript
复制
$smarty -> display('index.tpl');

我的查询执行了多次!:(

有关详细信息,请参阅:

http://www.smarty.net/forums/viewtopic.php?p=81161

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-07-17 16:23:09

一个常见的错误是在您的html输出中有一个空的img-tag或script-tag -这会使浏览器重新请求当前页面。

票数 1
EN

Stack Overflow用户

发布于 2012-07-17 02:30:34

为什么要调用$smarty->clear_compiled_tpl();?它确实会删除所有已编译的模板,并在每次调用页面时导致重新编译。删除此行。

您是在哪里以及如何调用函数计数器的?

票数 1
EN

Stack Overflow用户

发布于 2012-07-17 20:44:55

我的问题解决了:

我使用的幻灯片在java脚本代码中有下面这一行:

​

我在jquery.nivo.slider.js中将src="#“更改为src="”

它现在工作得很好,但是我真的不知道为什么浏览器工作起来很有趣!

谢谢

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11501621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档