2013-05-04 236 views
-2

我有一個AJAX調用,它通過JSON從文件中檢索行。我的數據庫中有3M行,我想顯示結果,但我什麼也看不到。用PHP緩存AJAX結果

通過JSON檢索結果我想緩存,這樣如果一個數據被多次請求,我不會再發送請求。什麼是jQuery緩存的最佳做法?

我在我的服務器端使用PHP。任何建議,將不勝感激。

我的JavaScript代碼:

function GetSearchResult(Char,Company,Category,Country,Page) 
{ 
    $.ajax({ 
     type: "POST", 
     url: "CompanyResult.php", 
     data: { 
      mySearchChar : Char, 
      mySearchCompany : Company, 
      mySearchCategory : Category, 
      mySearchCountry : Country, 
      myPage : Page 
       }, 
     cache: false, 
     dataType : "html", 
    beforeSend: function(){ 
     $("#Loading").show(); //show image loading 
     $('html, body').animate({scrollTop:$('#insid_body_web').offset().top}, 'slow'); 
     $("#ResultCompany1").hide(); 
     $("#ResultCompany2").hide(); 
     $("#ErrorSearch").hide(); 
    }, 
    complete: function(){ 
     $("#Loading").hide(); //hide image loading 
     $("#ResultCompany1").show(); 
     $("#ResultCompany2").show(); 
     $("#ErrorSearch").hide(); 
    }, 
    success: function(data){ 
    $(".insid_body_web").html(data); 
    $('html, body').animate({scrollTop:$('#insid_body_web').offset().top}, 'slow'); 
    $("#ResultCompany1").show(); 
    $("#ResultCompany2").show(); 
    $("#ErrorSearch").hide(); 
    $(".insid_body_web").css("min-height","1397px"); 
    }, 
    error: function() { 
    $("#ErrorSearch").show(); 
    $("#ResultCompany1").hide(); 
    $("#ResultCompany2").hide(); 
    }, 
}); 
} 

和PHP函數來得到結果:

public function selectallcompanydatabypagingbycat($char,$company,$cat,$country,$from,$records) 
{ 
    $sql ="select company_CompName,company_Phone,company_Site,company_id 
    from company 
    where 
    company_delete ='0' 
    and company_Request = '0' "; 
    if ($cat !=""){ $sql .= " and company_CompCat = '".$cat."' "; } 
    if ($company !=""){ $sql .= " and company_CompName = '".$company."' "; } 
    if ($country !=""){ $sql .= " and company_country = '".$country."' "; } 
    if ($char !=""){ $sql .= " and company_CompName like '".$char."%' "; } 
    $sql .= " order by company_id desc limit $from,$records"; 
    $query = @mysql_query($sql); 
    return $query; 
    mysql_free_result($query); 

} 
+0

請把一些精力投入到您的文章,告訴我們你是怎麼嘗試緩存,爲什麼你認爲這是行不通的。不只是你的代碼,並要求有人爲你寫。 – 2013-05-04 09:18:54

+0

@Puciek我有3000000行的數據庫,我不會顯示結果,但我什麼都看不到 – 2013-05-04 09:34:14

+0

你是否在將它們輸入到selectallcompanydatabypagingbycat'之前就沒有了你的AJAX輸入?一般來說,我會在該方法內部解開,因此完成。如果你沒有弄明白,你的代碼中有一個SQL注入。 – halfer 2013-05-04 10:23:39

回答

0

恰克你在這樣

data: { 
      "mySearchChar" : Char, 
      "mySearchCompany" : Company, 
      "mySearchCategory" : Category, 
      "mySearchCountry": Country, 
      "myPage" : Page 
       }, 

的Ajax和數據CompanyResult.php

$cat = trim($_POST['mySearchCategory']); 
$company = trim($_POST['mySearchCompany']); 
$country = trim($_POST['mySearchCountry']); 

...... 等等...