2012-06-05 64 views
0

我不知道爲什麼,但我收到此錯誤PHP的MongoDB沒有更新陣列

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

在該網頁上我有這個

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

這是怎麼了調用該函數

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); 
} 

或者你可以只使用$this->exchange_rates上面已經......在任何情況下,你沒有將$m可用的功能。

0

看起來像一個錯字:對象「$ M」是不是在功能updatemongo()

要麼在這裏創建它,如果它存在與global $m;獲得實例化。

0

$ this-> db-> exchangeDB在放入$ collection時包含null或類似內容?很難告訴我們,因爲我們不知道變量在哪裏被實例化。 $ array-> update(array(「from」=> $ from),$ data);

是您的錯誤的原因,錯誤很明顯:$ update不是一個對象。否則它會抱怨「PHP致命錯誤:調用未定義的方法YourClass :: yourMethod()in /my/php/file.php on line xx」