Mike Perrenoud
Mike Perrenoud

Reputation: 67948

Why am I getting "Call to undefined function findOne()" here?

I've got a little PHP script that has the following code in it:

$m = new MongoClient(Settings::$db);
$db = $m->db;

// get the 'customers' collection
$customers = $db->customers;

// determine if this customer already exists
$c = $customers.findOne(array('email' => $email)); <--- throws
if (is_null($c)) {
    $customers.insert(array(
        'email' => $email,
        'firstname' => $firstName,
        'lastname' => $lastName
    ));
}

// get the 'payments' collection
$payments = $db->payments;

// add a record
$payments->insert(array(
    'email' => $email,
    'firstname' => $firstName,
    'lastname' => $lastName,
    'amount' => $price,
    'token' => $token
));

but it's throwing the error:

PHP Fatal error: Call to undefined function findOne() in ...

Now, I've downloaded and copied into the ext directory the php_mongo.dll file. Further, I've added this line to the php.ini:

extension=php_mongo.dll

and I've since rebooted the server numerous times.

It really feels to me like this findOne method isn't available because the php_mongo extension isn't loaded. But on the other hand, it's not throwing when creating the MongoClient, nor is it throwing when grabbing the database and the collection.

What's going on here?

Upvotes: 0

Views: 1037

Answers (2)

Damien Pirsy
Damien Pirsy

Reputation: 25445

As per my comment, you likely used another language's method access syntax by mistake:

This

$c = $customers.findOne(array('email' => $email));

is alas PHP so you need the object operator not the dot:

$c = $customers->findOne(array('email' => $email));

Upvotes: 1

Vishnu
Vishnu

Reputation: 2452

I think dot is the mistake.try

$c = $customers->findOne(array('email' => $email)); 

Upvotes: 1

Related Questions