2012-05-27 36 views
0
<?php function ss($s1, $s2, $s3, $s4, $s5, $s6) { 
    $sonucArray = array("alti" => 0, "bes" => 0, "dort" => 0, "uc" => 0, "iki" => 0); 
    $sayiArray = array($s1, $s2, $s3, $s4, $s5, $s6); 
    $sayiArraySql = join(',',$sayiArray); 
    $query = mysql_query("select * from loto where 
            s1 in ($sayiArraySql) or 
            s2 in ($sayiArraySql) or 
            s3 in ($sayiArraySql) or 
            s4 in ($sayiArraySql) or 
            s5 in ($sayiArraySql) or 
            s6 in ($sayiArraySql)"); 

    while ($record = mysql_fetch_array($query)) { 
     $controlArray = array($record["s1"],$record["s2"],$record["s3"],$record["s4"],$record["s5"],$record["s6"]); 
     $diffArray = array_diff($sayiArray,$controlArray); 

     if (count($diffArray) == 0) 
      $sonucArray["alti"]++; 
     else if (count($diffArray) == 1) 
      $sonucArray["bes"]++; 
     else if (count($diffArray) == 2) 
      $sonucArray["dort"]++; 
     else if (count($diffArray) == 3) 
      $sonucArray["uc"]++; 
     else if (count($diffArray) == 4) 
      $sonucArray["iki"]++;  
    } 

    return $sonucArray; 
} 

mysql_fetch_array;使用太多CPU並且不快!針對MySQL查詢的優化 - 獲取PHP

「s1-s2-s3-s4-s5-s6」是整數。

取而代之的應該是一個代碼?我知道PHP不足。有人建議我「mysql_fetch_row」,但我無法通過修改

我應該怎麼寫?

+0

如果您的查詢太慢,請嘗試索引您正在搜索的列。 –

+0

這些列已編入索引。有很多查詢處理 – user1419633

回答

0

你的使用和mysql_fetch_row功能:

<?php 
function ss($s1, $s2, $s3, $s4, $s5, $s6) 
{ 
    $sonucArray = array("alti" => 0, "bes" => 0, "dort" => 0, "uc" => 0, "iki" => 0); 
    $sayiArray = array($s1, $s2, $s3, $s4, $s5, $s6); 
    $sayiArraySql = join(',',$sayiArray); 
    $query = mysql_query("select s1, s2, s3, s4, s5, s6 from loto where 
           s1 in ($sayiArraySql) or 
           s2 in ($sayiArraySql) or 
           s3 in ($sayiArraySql) or 
           s4 in ($sayiArraySql) or 
           s5 in ($sayiArraySql) or 
           s6 in ($sayiArraySql)"); 

    while ($record = mysql_fetch_row($query)) 
    { 
     $controlArray = array($record[0],$record[1],$record[2],$record[3],$record[4],$record[5]); 
     $diffArray = array_diff($sayiArray,$controlArray); 

     if (count($diffArray) == 0) 
      $sonucArray["alti"]++; 
     else if (count($diffArray) == 1) 
      $sonucArray["bes"]++; 
     else if (count($diffArray) == 2) 
      $sonucArray["dort"]++; 
     else if (count($diffArray) == 3) 
      $sonucArray["uc"]++; 
     else if (count($diffArray) == 4) 
      $sonucArray["iki"]++;  
    } 
    return $sonucArray; 
} 

和mysql_fetch_row只是使用整數,而不是字段名稱爲索引的數組返回。根據您的sql請求,$ record [0]將成爲行中的第一個字段。這就是爲什麼我在請求中寫入每個字段名稱而不僅僅是*,以確保順序對應。

希望這有助於。