PHP Mongodb不更新数组
我不知道为什么,但我得到了这个错误PHP Mongodb不更新数组,php,mongodb,mongodb-php,Php,Mongodb,Mongodb Php,我不知道为什么,但我得到了这个错误 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; $c
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”)代码>
请告诉我这不起作用的原因。看起来像是输入错误:对象“$m”没有在函数updatemongo()中实例化。
在此处创建或使用global$m获得访问权限代码>如果存在。$this->db->exchangeDB在放入$collection时是否包含null或类似内容?我们很难说,因为我们不知道该变量在哪里被实例化
$collection->update(数组(“from”=>$from),$data)
是错误的原因,错误很明显:$update不是对象。否则它会抱怨“PHP致命错误:调用xx行/my/PHP/file.PHP中未定义的方法YourClass::yourMethod()”函数无法访问$m
变量。请将其传递给函数,如下所示:
$fetch->updatemongo($m, $from, $newdata);
并将函数定义更改为:
公共函数updatemongo($m,$from,$data)
{
或者,您可以在创建连接后将对象的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\u rates
。。在任何情况下,您都不能使$m
对该函数可用