2012-07-16 43 views
0

我使用Smarty的時間多用這種設置:SQL查詢執行比智者

$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']."' ;") ; 

我認爲智者有someth要解決我的問題!

當我寫這篇文章的代碼:

$q=$smarty -> fetch('index.tpl'); 

查詢執行1次但是當我我的代碼更改爲:

$q=$smarty -> fetch('index.tpl'); 
echo $q; 

$smarty -> display('index.tpl'); 

我的查詢執行超過一次!:(

瞭解更多信息:

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

+0

如果實際輸出任何東西都是問題的原因,那不是聰明。你有一個過濾器的輸出緩衝區?或一些關機功能? – rodneyrehm 2012-07-16 20:15:34

+0

我不明白你的意思 – 2012-07-17 06:51:43

+0

你可以告訴我們index.tpl嗎? – sofl 2012-07-17 11:11:39

回答

0

一個常見的錯誤是在您的html輸出中有一個空的img標籤或腳本標籤 - 這會使瀏覽器重新請求當前頁面。

+0

爲什麼當我不使用smarty時,問題並沒有解決,是否存在? – 2012-07-17 10:00:31

1

爲什麼你調用$ smarty-> clear_compiled_tpl();? 它會刪除所有已編譯的模板,並在每次調用頁面時都會導致重新編譯。 刪除此行。

你在哪裏和如何叫功能計數器?

+0

我已經刪除了clear_compiled_tpl,不幸存在問題!當我刪除$ smarty - > display('index.tpl');沒有問題! – 2012-07-17 06:39:49

+0

我的程序在Opera中正常工作!我的問題似乎與此提示有關:**您的輸出中很可能有一個空的img-src-或script-src-或link-href-stylesheet-attribute。 瀏覽器嘗試加載相對於當前URL的圖像,並使其第二次獲取您的index.php。**我該如何解決這個問題:(? – 2012-07-17 08:02:20

0

我的問題就解決了:

我用幻燈片放映具有此行Java腳本代碼:

我改變了SRC = 「#」 爲src = 「」 在jQuery的.nivo.slider.js

現在它工作正常,但我真的不知道爲什麼瀏覽器工作有趣!

謝謝

1

Smarty的僅僅是一個模板引擎,它不應該導致數據庫問題。你的問題可能在模板引擎之外的某個地方。