2012-08-01 4 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

+0

네, 지금까지 저는 alfa = 2로 디버깅 중입니다. – lollo

답변

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

+0

.. 아주 좋은 생각입니다, 감사합니다 .. 그러나 나는 작은 변화'$ insertVals로 실행했습니다. = ($ insertVals == ""? "": ",") ..... ecc .... (앞의 점에주의하십시오) bye – lollo