2012-06-05 3 views
0

를 업데이트하지 나는이PHP MongoDB를 배열

public function updatemongo($from,$data) 
    { 
     $this->db = $m->exchange_rates; 
     $collection = $this->db->exchangeDB; 
     $collection->update(array("from" => $from), $data); 
    } 

이 내가이 함수를 호출하고 어떻게해야하지만 해당 페이지에이 오류

Fatal error: Call to a member function update() on a non-object in /home/XXXXXXXXXXXXXXXXX/classes/core.php on line 22 

을 받고있는 이유 확실하지 않다

foreach ($currencies as $to) 
{ 
    if($from != $to) 
    { 

     $url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X'; 
     $handle = fopen($url, 'r'); 

     if ($handle) { 
      $result = fgetcsv($handle); 
       fclose($handle); 
     } 

     $newdata = array('$set' => array("exchangehistory.{$result[1]}.{$result[2]}" => array("to" => $to, "rate" => $result[0], "updated" => $result[2]))); 
     $fetch->updatemongo($from,$newdata); 

     $newdata = array('$set' => array("currentexchange" => array("to" => $to, "rate" => $result[0], "updated" => $result[2]))); 
     $fetch->updatemongo($from,$newdata); 


    } 
} 

예,이 파일에 액세스해야하는 파일은 require_once("core.php");

입니다.

이것이 작동하지 않는 이유를 알려주십시오.

답변

0

updatemongo() 함수는 $m 변수에 액세스 할 수 없습니다.

$fetch->updatemongo($m, $from, $newdata); 

을하고 함수 정의를 변경 :이 같은 함수에 전달하십시오 {

또는

공공 기능 updatemongo ($ m에서 $, $ 데이터)을 수행 할 수 있습니다 개체를 만든 후 m 속성을 연결에 설정하십시오. 로 예를 들면 :

public function __construct) 
{ 
    $this->m = new Mongo(); 
} 
... 
public function updatemongo($from, $data) 
{ 
    $this->db = $this->m->exchange_rates; 
    $collection = $this->db->exchangeDB; 
    $collection->update(array("from" => $from), $data); 
} 

아니면 당신은 어떤 경우에, 당신은 기능을 사용할 수 $m을하지 않는 ... 이미 위의 $this->exchange_rates를 사용할 수 있습니다.

0

오타처럼 보이는 : 오브젝트 "$의 m은"updatemongo()

는 여기를 만들거나 존재하는 경우 global $m;에 액세스 어느 함수에서 인스턴스화되지 않습니다.

0

$ this-> db-> exchangeDB에 $ collection에 넣을 때 null 또는 유사한 값이 포함되어 있습니까? 변수가 어디에서 인스턴스화되는지 알 수 없기 때문에 우리에게 알려주는 것이 어렵습니다.

$ collection-> update (array ("from"=> $ from), $ data);

이 오류의 원인이며 오류가 명확합니다. $ update는 개체가 아닙니다. 그렇지 않으면 "PHP 치명적인 오류 : 정의되지 않은 메서드를 호출합니다 .xml의 /my/php/file.php에서 YourClass :: yourMethod()를 호출하십시오."