2012-08-01 39 views
0

如何設置此功能的工作?最後一個查詢沒有得到執行..我已經在循環中搜索了很多查詢(foreach,for,while),但沒有任何..我試圖存儲會話。多查詢+單查詢

private function gc($expire) 
{ 
    $gcq = "SELECT `path`, `last`, LENGTH(`path`) FROM `sessions` WHERE LENGTH(`path`) > 0 AND DATE_ADD(`last`, INTERVAL ".(int) $expire." SECOND) < NOW();"; 
    $gcq .= "DELETE FROM `sessions` WHERE DATE_ADD(`last`, INTERVAL ".(int) $expire." SECOND) < NOW()"; 
      if($this->dbh->multi_query($gcq)) 
       { 
       $arr_gc = null; 
       $count = 0; 
       do { 
         if($result = $this->dbh->store_result()) 
         { 
         while($row = $result->fetch_assoc()) 
         { 
         $arr_gc[$count] = array($row['path'], $row['last']); 
         $count++; 
         } 
         $result->free(); 
         } 
         if($this->dbh->more_results()) 
         { 
         $garbage = null; 
         } 
        } 
        while($this->dbh->next_result()); 
        } 
        // no problems up here.. 
        // problems from here to end....   

        $alfa = count($arr_gc); 

        if($alfa > 0) 
        { 
         $count = 0; 

         while($count < $alfa) 
         { 
         $this->dbh->query("INSERT INTO `store_sess` SET `xpath` = '".$this->dbh->real_escape_string($arr_gc[$count][0])."', in = '".$this->dbh->real_escape_string($arr_gc[$count][1])."', out = '0000-00-00 00:00:00'"); 
         $count++; 
         }       
        } 
    return $this->dbh->affected_rows; 
} 

編輯: store_sess表的結構:

id int (autoincrement) 
xpath longtext 
in datetime (tried also with varchar) 
out varchar 
+0

你肯定'$ alfa'> 0,對不對? – Matt 2012-08-01 19:58:18

+0

是的,現在我正在調試alfa = 2 – lollo 2012-08-01 20:02:17

回答

0
while($count < $alfa) { 
    $this->dbh->query("INSERT INTO `store_sess` SET `xpath` = '".$this->dbh->real_escape_string($arr_gc[$count][0])."', in = '".$this->dbh->real_escape_string($arr_gc[$count][1])."', out = '0000-00-00 00:00:00'"); 
    $count++; 
} 

上面的代碼是不是很可擴展性。相反,您應該完整地設置您的查詢(您可以使用一個查詢執行多個插入操作),並在查詢字符串設置完成後執行它。

UPDATE

while循環是不必要也。

結束時更新

$insertVals = ""; 
for($count = 0; $count < $alfa, $count++) { 
    $insertVals = ($insertVals == "" ? "" : ", ") . 
     "('" . $this->dbh->real_escape_string($arr_gc[$count][0]) . "', '" . 
     $this->dbh->real_escape_string($arr_gc[$count][1]) . 
     "', '0000-00-00 00:00:00')"; 
} 

$query = "INSERT INTO `store_sess` (`xpath`, `in`, `out`) VALUES " . $insertVals; 
+0

謝謝...稍後會嘗試... :)再見 – lollo 2012-08-01 20:01:19

+0

..非常好的主意,謝謝..但是我用小改動'insertVals來運行它。 =($ insertVals ==「」?「」:「,」).....'ecc ....(注意=之前的小圓點)bye – lollo 2012-08-02 14:26:33