0

I am not sure why but I am getting this error

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

On that page I have this

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

this is how I am calling this function

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


    }
 }

and yes the file needing to access this is also has require_once("core.php");

Please let me know why this is not working.

3 Answers 3

0

The updatemongo() function doesn't have access to the $m variable. Please pass it to the function like this:

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

And change your function definition to:

public function updatemongo($m, $from, $data) {

Alternatively, you can set the m property of the object to the connection after you've created it. For example with:

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

Or perhaps you can just use $this->exchange_rates already above... in any case, you're not making $m available to the function.

0

Looks like a typo : the object "$m" is not instantiated in function updatemongo()

Either create it here or gain access with global $m; if it exists.

0

$this->db->exchangeDB contains null or similar when it gets placed into $collection? It's hard to tell for us since we don't know where that variable gets instantiated.

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

is the cause of your error and the error is clear: $update is not an object. otherwise it would complain "PHP Fatal error: Call to undefined method YourClass::yourMethod() in /my/php/file.php on line xx"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.